Improve Wordpress .htaccess

Url rewriting is very (very very) important for seo and Wordpress users can easily avoid ugly urls like ?p=43 using permalinks in their installations.

To achieve the result of a true rewrite (no index.php file in the url) wordpress requires apache mod_rewrite module, and uses an .htaccess file that has the only purpose to redirect everything that does not exists physically to your index.php file. The default htaccess file looks something like that

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

Apart from the comments let's see quickly what most important line means

  • <IfModule mod_rewrite.c>

This just checks if your apache server has mod_rewrite installed and enabled

  • RewriteEngine On

Enables the rewriting engine

  • RewriteBase /

Sets the base url for the rewrite rules

  • RewriteCond %{REQUEST_FILENAME} !-f

Checks that the path requested is not present on your server.  This means that the server will  redirect on the index.php just addresses that do not match any physical file.

  • RewriteCond %{REQUEST_FILENAME} !-d

Checks that the path requested is not present on your server.  This means that the server will  redirect on the index.php just addresses that do not match any physical directory.

  • RewriteRule . /index.php [L]

Tells the server to redirect anything to the index.php file.

Let's do some examples to clarify how does it work exactly. Imagine that the following files are your wordpress directory content

index.php somepic.png

So if an user come to your websites with this url

http://www.yoursite.com/an-article.html

he will be redirected to the index.php file which will show the article, as the file does not exist on your server.

If instead an user arrives to

http://www.yoursite.com/somepic.png

he won't be redirected because that image exists. He will just see the image, and this is correct.

Now suppose that someones arrive in your page

http://www.yoursite.com/an-article.html

and that in your template there is an inclusion of a js script (not present on your server) called

myscript.js

Including a script is like making an other http request to the server so it's just like if the browser opened this page

http://www.yoursite.com/myscript.js

Wordpress htaccess file, will redirect the user to the index.php as the JS does not exists!!

This means wordpress will try to treat that JS as an article and so it will search on its database for any article having that name.

This  means that opening http://www.yoursite.com/an-article.html with a missing js file has nearly the same weight of calling two pages!

This is a big problem when you got an heavy installation of Wordpress with a lot of traffic, because it's like doubling the traffic.

Yes that's obviously an human error that can be avoided as you should always check for every image,js and css to exist.

However I prefer being sure that such problems do not verify so I just added some more rules to my htaccess,before the file and directory existence checks.

RewriteCond %{REQUEST_FILENAME} !^.*\.png [nc]  
RewriteCond %{REQUEST_FILENAME} !^.*\.css [nc]  
RewriteCond %{REQUEST_FILENAME} !^.*\.jpg [nc]  
RewriteCond %{REQUEST_FILENAME} !^.*\.js [nc]  
RewriteCond %{REQUEST_FILENAME} !^.*\.gif [nc]

As you can guess it's just an other check before doing the redirects. If the file requested is not a css, a js or an image (gif,png, or jpg) then do the redirect.

So if any user come to my website and opens a page which contains an image that is no more there Apache will just launch a 404 not found error but won't weigh down the server.

So here's my full htaccess file, I bet you just wanted to see that without the explanations uh?

# BEGIN WordPress  
<IfModule mod_rewrite.c>
RewriteEngine On  
RewriteBase /  
RewriteCond %{REQUEST_FILENAME} !^.*\.png [nc]  
RewriteCond %{REQUEST_FILENAME} !^.*\.css [nc]  
RewriteCond %{REQUEST_FILENAME} !^.*\.jpg [nc]  
RewriteCond %{REQUEST_FILENAME} !^.*\.js [nc]  
RewriteCond %{REQUEST_FILENAME} !^.*\.gif [nc]  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule . /index.php [L]  
</IfModule>
# END WordPress
apache, Wordpress
Andrea Olivato