SEO URL’s 404 Alternative

April 2nd, 2008 | by Jez |

Many websites using search engine optimised URL’s require a single point of entry and for this they use the Apache ErrorDocument 404 call. This means every page is redirected to a control page which can then analyse the URI Request and decide which page to load.

In my experience with this technique I have come across numerous problems, mainly that the form POST method will not work. After research it appears this is because the data is being redirected to a 404 error, so the information is lost. For most people this isn’t a problem, as they can use separate pages which exist to analyse the form data, however this defeats the object of keeping the single point of entry for the website.

A solution to this problem is using the RewriteEngine, we can rewrite every requested URL to the entry script.

  1. Options +FollowSymLinks
  2. RewriteEngine on
  3. RewriteBase /
  4. RewriteRule ^(.*)$ index.php

This will effectively rewrite every request to “index.php” no matter what file it is, which obviously isn’t good if we want to be able to load images and css files into the page. A simple way to achieve this, is with some RewriteConditions.

  1. Options +FollowSymLinks
  2. RewriteEngine on
  3. RewriteBase /
  4. RewriteCond %{REQUEST_URI} !images(.*)$
  5. RewriteCond %{REQUEST_URI} !css(.*)$
  6. RewriteCond %{REQUEST_URI} !js(.*)$
  7. RewriteRule ^(.*)$ index.php

As you can see this will check if the words images, css or js exist within the requested URI. This is great, however everytime we want to exclude a page or directory from our script we will have to add an exception to the htaccess file. Luckily apache gives us a wonderful feature to check if the file actually exists on the local filesystem.

  1. RewriteEngine On
  2. RewriteBase /
  3. RewriteCond %{SCRIPT_FILENAME} !-f
  4. RewriteCond %{SCRIPT_FILENAME} !-d
  5. RewriteRule ^(.*)$ index.php

The -f means the filename, so by checking if it exists within the requested script filename we can decide whether or not to include our base handler script. The “!” simply means “not” so if the file doesnt exist continue with the conditions, where as if it does exist, load that file instead. The next line (”-d”) means check if the directory path exists and if so, load that rather than calling the handling script.

Thats all there is to it, and now form data can be posted backwards and forwards without any issues!

Sorry, comments for this entry are closed at this time.