logo


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
  • antonio

    ciao

    intanto complimenti per il sito molto bello, sto cercando di capire questo post e forse ci riuscirò, sono un neofita quindi mi perdonerai se la domanda che ti faccio ti sembrerà banale, forse la risponda è in questo post, ed io non la vedo, comunque questa è la domanda:

    di solito quando si installa wordpress in un server questo è inserito in una cartella secondaria tipo "blog" e non nella root esempio http;//miosito.it/blog e si entra nel blog,
    ma se io voglio far partire il blog direttamente da questo indirizzo come si fa ?http://miosito.it/

    grazie della risposta

  • antonio

    ciao

    intanto complimenti per il sito molto bello, sto cercando di capire questo post e forse ci riuscirò, sono un neofita quindi mi perdonerai se la domanda che ti faccio ti sembrerà banale, forse la risponda è in questo post, ed io non la vedo, comunque questa è la domanda:

    di solito quando si installa wordpress in un server questo è inserito in una cartella secondaria tipo "blog" e non nella root esempio http;//miosito.it/blog e si entra nel blog,
    ma se io voglio far partire il blog direttamente da questo indirizzo come si fa ?http://miosito.it/

    grazie della risposta

  • http://blog.andreaolivato.net Andrea Olivato

    Ciao Antonio,

    non devi fare nulla di particolare per installare wordpress sulla root del tuo sito. Semplicemente quando estrai i file dal .zip di wordpress, anzichè lasciarli dentro la cartella che ti si viene a creare li uploadi direttamente dentro la cartella principale del sito…. in questo modo il tutto sarà installato dove desideri ed anche l'indirizzo sarà quello che richiedi…

    Spero di essermi spiegato, se avessi qualche altro dubbio dimmi pure…

  • http://intensedebate.com/people/andreaolivato andreaolivato

    Ciao Antonio,

    non devi fare nulla di particolare per installare wordpress sulla root del tuo sito. Semplicemente quando estrai i file dal .zip di wordpress, anzichè lasciarli dentro la cartella che ti si viene a creare li uploadi direttamente dentro la cartella principale del sito…. in questo modo il tutto sarà installato dove desideri ed anche l'indirizzo sarà quello che richiedi…

    Spero di essermi spiegato, se avessi qualche altro dubbio dimmi pure…

  • http://watchfreemoviesnow.org/ watch movies

    This really helped me a lot. On my website wordpress has a picture folder. I knew this normal rewrite command but after putting that command wordpress admin panel didn't show the preview of the pictures in thumbnail. So i put that extra image rewrite functions and now it shows those preview of pictures in thumbnails. Thank you very much.

  • Pingback: 100+ Massive Wordpress Tutorial Collection - tripwire magazine