.htaccess rewrite simple tutorial

Apache webserver interprets the requests it receive as URLs. The default behaviour is to translate /a/certain/path into as a UNIX path, looking for the oject “path” in the “a/certain” folder starting from the “document root” (the path in the server that Apache knows to be the root, such as /var/www/yourdomain.com/).

There is a complicated configuration file that can be modified to overcome this behaviour: .htaccess that has to be placed in the website root folder (or in the subfolder we want to affect the behaviour of).

Changing error messages

To change server errors like the “404 page not found” can add the following lines to the .htaccess file:

ErrorDocument 401 /errores/where-is-your-password.html
ErrorDocument 403 /errors/permission-denied.htm
ErrorDocument 404 /errors/404.htm
ErrorDocument 500 /errors/server-error.htm

In this example we are telling Apache were to look for an error. Now consider the

/errors/404.htm page:

<h2>4ngs.com</h2>
<h1>The desired page was not found</h1>
<p><img src="../images/t.png">his is sad, 
but we will do our best to make this site better</p>

If the user try reaching http://www.yourdomain.com/nonexistingpage.html everything works. Troubles begin with http://www.yourdomain.com/page/notfound.html: the image will not display.
Remember not to use relative path in this pages…

Redirecting URLs

Suppose you have an index.php file that can serve all the pages of your site via a page parameter like:

http://www.domain.org/index.php?page=about

We can exploit the power of .htaccess file to have requests like

http://www.domain.org/page/about

to be redirected to the former URL. This requires pattern matching and is handled by this directive:

RewriteEngine on
RewriteRule ^page/(.*)$ index.php?page=$1 [NC]
 

We turn on the “RewriteEngine” that process a URL request and then we can add as many “rules” as needed. The example given is quite clear and uses this general syntax:

 RewriteRule pattern  page [options]

As pattern we use ^page/(.*)$ where ^ and $ acts as in any regular expression asking the pattern to match the whole URL. Also the round brackets acts as usual capturing its content. Then the backreference $1 is added in the redirection.

 [NC] ask to have the pattern treated as case-insensitive

 Also in this case we have the caveat about relative links, that should be avoided.

Advertisements