WordPress Code Snippets

A fresh mix of code snippets and tutorials

.htaccess template file for WordPress

36

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 a root .htaccess file.

Default .htaccess rules for WordPress

The .htaccess required for WordPress permalinks looks like this:

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

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

<Files .htaccess>
	Order Allow,Deny
	Deny from all
</Files>
<Files wp-config.php>
	Order Deny,Allow
	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]

# supress 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

# 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>

Learn more

WordPress Themes In Depth


Show Support

Like our new Facebook Page to show support!