1 minute read

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.

Comments