Friendly URLs (revisited)

Turn dynamic URLs into friendly URLs

I’m sure we’re all familiar with URLs that look like this:

http://www.example.com/?nav=page

These type of URLs aren’t particularly “friendly”, they are known as dynamic URLs. As a rule of thumb search engines such as Google don’t like them as much as “static URLs”.

However, Google has recently released an article on this very subject entitled Dynamic URLs vs. static URLs, I recommend you give it a read so you fully understand what we’re talking about.

Google suggests that many search engine crawlers do not like dynamic URLs as much as static URLs.

A “static” or “friendly” version of the above URL could be as follows:

http://www.example.com/page.html

Here’s how it’s done…

Solution 1

Apache’s mod_rewrite can be easily used via a file called “.htaccess” to turn dynamic urls into friendly urls.

Here is an example of how it’s done:

#Turn on the Rewrite Engine
RewriteEngine on
#Set the base path
RewriteBase /
#Check that the lookup isn’t an existing file
RewriteCond %{REQUEST_FILENAME} !-f
#Check that the lookup isn’t an existing directory
RewriteCond %{REQUEST_FILENAME} !-d
#Check that the file isn’t index.php (avoid looping)
RewriteCond %{REQUEST_URI} !^index\.php$
#Force all .html lookups to the index file
RewriteRule (.+)*\.html index.php?nav=$1 [QSA,L]
#Note: QSA=query string append;L=Last, no more rules

This will rewrite all paths ending in “.html” to your index file.

From there, it’s simply a case of tailoring the rewrite to your requirements.

Checkout the mod_rewrite cheat sheet for more help on rewrites.

Solution 2

If you ARE using PHP, a better way might be to just hand over ALL the path information to your “index.php” and handle it from there.

The rewrite to do that looks something like this:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteRule ^(.+)$ index.php/$1 [QSA,L]

As per above this will only rewrite paths that don’t exist.

It doesn’t work out any slower than the above solution, as either way you’re passing it to PHP, and rewrites are fairly slow to begin with.

In your “index.php”, you can parse $_SERVER['PATH_INFO'] (or $_SERVER['ORIG_PATH_INFO']) for the path information. It may be quicker and easier to explode the path by “/”, and find the information you need using a foreach rather than using regex in preg_match.

Related posts:

  1. Creating subdomains from directories using mod_rewrite in Apache .htaccess The idea was to have the ability to create unlimited...
  2. Word separators in URLs In the world of web development and search engine optimisation...
  3. 50+ PHP optimisation tips revisited After reading an article some time ago entitled “40 Tips...
  4. suPHP and .phps PHP code highlighting support Today a user on one of my web servers asked...

3 Comments »

  1. Ben Steele said,

    January 28, 2007 @ 10:52 am

    Multiviews is also an option here, although it takes a little bit more time preparing your code the finished url output is excellent.

  2. Zeeshan said,

    August 1, 2008 @ 2:30 pm

    I would have to agree. Using a resource extensive module to act as glue between your application and the HTTPd is wasteful. Consider using the PATH_INFO environment via MultiViews, as Ben Steele highlighted. It’s also possible to use it via SetAction and ForceType.

  3. hm2k said,

    November 12, 2008 @ 12:55 pm

    I don’t like MultiViews, second guessing isn’t really something I like my web server to do, further more rewrites are so much more powerful.

RSS feed for comments on this post · TrackBack URL

Leave a Comment