.htaccess template file for WordPress
By default, WordPress does not require any .htaccess rules to function properly. But if you want to enable pretty permalinks, a simple set of .htaccess rules must be included in your site’s root .htaccess file.
Default .htaccess rules for WordPress
The .htaccess required for WordPress permalinks looks like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
To use this code, you’ve got to add/create an .htaccess file in the root directory of your site. So if you’re going to do this, there are some other useful .htaccess techniques that will be advantageous to include as well. Let’s combine them with the permalink rules to create our own custom .htaccess template file for WordPress.
Complete .htaccess template file for WordPress
Okay, so we’ve got our permalink rules and now want to improve functionality with some useful .htaccess techniques. So without further ado, here is our complete .htaccess template file:
RewriteEngine On
Options -Indexes
Header unset ETag
FileETag None
<FilesMatch "\.ht(.*)">
Order Allow,Deny
Deny from all
</FilesMatch>
<Files wp-config.php>
Order Allow,Deny
Deny from all
</Files>
# secure svn stuff
RewriteRule ^(.*/)?\.svn/ - [F,L]
# www canonical urls
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# WordPress permalinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [PT]
# cache control
ExpiresActive On
ExpiresDefault A0
<FilesMatch "\.(gif|jpg|jpeg|png|swf)$">
ExpiresDefault A1209600
Header append Cache-Control "public"
</FilesMatch>
<FilesMatch "\.(xml|txt|html)$">
ExpiresDefault A7200
Header append Cache-Control "proxy-revalidate"
</FilesMatch>
<FilesMatch "\.(js|css)$">
ExpiresDefault A259200
Header append Cache-Control "proxy-revalidate"
</FilesMatch>
# suppress php errors
# php_flag display_startup_errors off
# php_flag display_errors off
# php_flag html_errors off
# php_value docref_root 0
# php_value docref_ext 0
# log php errors
# php_flag log_errors on
# php_value error_log /absolute/path/to/error.log
Note that the template includes two sections, “suppress php errors” and “log php errors”, that require server support in order to work. This is why the directives for these two sections are commented out (disabled) by default. Before enabling either of these sections, be sure to confirm that your server supports the directives.