Search Engine Friendly URLs
Category: PHPReviewed by: redemption
Reviewed on: Oct 11 2003
» Discuss this topic ( Posts)
More advanced usages
If you want to look more professional it's likely that you want to use a URL
that looks more like this:
http://www.domain.com/files/members/us/ca/5/
This is also possible, and there are two methods to doing this.
a) SetHandler directive in Apache
This method, in my opinion, is the simplest and most convenient. This only works though if your server allows you to override functionality using .htaccess, or if you can access Apache's httpd.conf files directly. At this time as far as I am aware this method really only works for Apache.
Personally I recommend the .htaccess approach, so in the files
directory put this in the .htaccess file (if no .htaccess file exists, just
create one):
[code]
SetHandler application/x-httpd-php
ForceType application/x-httpd-php
Then, instead of saving your members script as " members.php ", save it only as " members " (with no file extension at all) in that directory. What the above is doing is telling Apache to treat the /files/members/ directory as a call to the " members " file.
b) Mod Rewrite
An alternative to SetHandler is the use of ModRewrite. ModRewrite is a very powerful tool that allows you to rewrite URL requests such that Apache treats a call on one path as a request for a totally different path. Using ModRewrite you can perform redirects, and also mask the true path of a script being executed. You can also use it for SEF URLs :).
ModRewrite requires that you have the module installed for it to function. You can see whether ModREwrite is enabled on your server through the phpinfo() function - load up a page with phpinfo() and then search for "mod_rewrite" under the Apache section.
Next, you need to include the following in an .htaccess file or else in your Apache httpd.conf file: [code]
RewriteEngine On
#turn on the Rewrite engine, if it's not already active
#set the base directory to /files
RewriteBase /files/
# now the rewriting rules
RewriteRule ^members/ members.php [L]
RewriteRule ^sections/ directory.phpl [L]
RewriteRule ^cities/
directory.php [L]
RewriteRule ^maps/ maps.php [L]
[/code]
The above activates mod_rewrite and tells it to treat calls to each of the RewriteRule directories as calls to actual files instead... essentially rewriting the URI request so that it's requesting those files. The [50] portions following each line indicate to mod_rewrite that once that rule is satisfied it can ignore the other rules.
Mod_rewrite is a very complicated, robust, and useful module that is better explained by the mod_rewrite page.
c) SymLinks
One other option is to use Linux symlinks to mask the filename and simulate a directory. For this to work you must have FollowSymLinks enabled in Apache or in your .htaccess. This method is the least favoured in my opinion because you might run into problems with permissions with symlinks that execute files.
In this example you can symlink your members.php file like so:
ln -s members.php ./members
Again, this technique is merely another alternative. The SetHandler and Mod_Rewrite methods are by far the ones I use the most.
Conclusion & What's Next
Well that's it! Search Engine Friendly URLs are easy and quick to implement. They are also much easier on the eyes than ugly looking GET strings. If done properly you can even make reasonably human readable URLs, say by using a unique Username key as a part of your URL rather than memberids:
http://www.domain.com/files/members/Jack/
or even
http://www.domain.com/files/members/Jack.html
The above model can be extended further for things like articles, products, forum threads, and more. DEVPEN for instance uses unique filenames to identify articles. You can just as easily read this SEF article using a URL like http://www.devpen.com/articles.php?a="3.
If I see the need for further explanation of how to do these last few examples I will extend this article... for now I think you have the tools and ideas to make all sorts of useful SEF URLs.
And for naysayers who believe that SEF URLs are not worth it because of the added load of parsing out the REQUEST_URI string, and the load of the Mod_Rewrite (if you are using mod_rewrite), I think you just have to balance your desire of having beautiful, search engine friendly and more human intuitive URLs versus the very small added load that taking such an approach creates. I've used SEF URLs on sites that have HUGE numbers of requests per hour, and yes you do end up using more horsepower, but you also have much nicer looking URLs on search engines such as Google, which show part of the URL in their search results.
Add all that to the fact that some search engines still do not spider CGI like pages fully (especially beyond the first or second depth of links) makes SEF URLs a worthwhile technique to use on your next site.

