PhpRiot
Download This Article
Download this article in PDF format with all listings and files.

Price: $5.00 AUD
(Approx. $4.10 USD)

More information
Related Books
Professional Search Engine Optimization with PHP: A Developer's Guide to SEO

Professional Search Engine Optimization with PHP: A Developer's Guide to SEO

Maybe you’re a great programmer or IT professional, but marketing isn’t your thing. Or...

Search Engine Optimization for Flash: Best practices for using Flash on the web

Search Engine Optimization for Flash: Best practices for using Flash on the web

Some people think that Flash-based websites and Rich Internet Applications won't show up in a web...
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (12), JSON (2), MVC (1), MySQL (7), onbeforeunload (1), OOP (1), PHP (28), PhpDoc (1), PostgreSQL (6), Prototype (11), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

Creating Search Engine Friendly URLs In PHP

Using mod_rewrite as a 404 Handler

While the custom 404 handler is one of the most flexible methods for creating search-engine friendly URLs, the biggest problem with it when it comes to Apache is that POST data is not available (due to way Apache directs the request data when using ErrorDocument).

To get around this, we can use a combination of mod_rewrite and the custom 404 handler. The way this technique works is to use mod_rewrite to direct all requests to the specified script when the requested file was not found. This is achived using the following code (either in your .htaccess or httpd.conf.

Listing 10 Using mod_rewrite to foward requests that don't correspond to an existing file or directory (.htaccess)
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1

The first RewriteCond checks if the requested URL exists as a file on the filesystem. In mod_rewrite, -f refers to a file, while the bang is the "not" operator. Therefore, !-f means "if the request script filename does not exist as a file".

If the first rewrite_cond succeeds (that is, the requested file wasn't found), then Apache moves on to the next condition. The !-d directive means it the condition succeeds if the requested script does correspond to an existing directory on the server.

Finally, if both conditions succeed, the request is forwarded to the index.php file, as specified in the RewriteRule directive.

You can use the techniques in the previous section (on implementing a custom 404 handler) to read the URL and act appropriately. Using the technique outlined on this page you can now access request POST data.

In This Article


Tagged in ,

Article History

Jan 10, 2006
Initial article version
Feb 27, 2008
Added the "Using mod_rewrite as a 404 Handler" page to the article