Tuesday 5 May 2009

WordPress' .htaccess file explained

If you have configured your WordPress system to use pretty permalinks the following will be added to the .htaccess file:

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Explaining the .htaccess, row by row

<IfModule mod_rewrite.c>
This line checks if the mod_rewrite module is available on the server. If not, none of the enclosed Rewrite commands will be processed.

RewriteEngine On
This directive enables the runtime rewriting engine.

RewriteBase /
Let's the server know that the .htaccess was reached via / and not through any other path prefix.

RewriteCond %{REQUEST_FILENAME} !-f
This condition is true if the the path REQUEST_FILENAME not refers to an existing file.

RewriteCond %{REQUEST_FILENAME} !-d
This condition is true if the the path REQUEST_FILENAME not refers to an existing directory

RewriteRule . /index.php [L]
If the two RewriteCond's listed above evaluated to true the server will load index.php. [L] indicates that no further rewrite rules should be processed.
If any of the RewriteCond's evaluated to false, the server will load the actual file or directory instead of index.php.

No comments:

Post a Comment