Thursday, March 17, 2011

The Apache "mod_rewrite": Best SEO Tool, Must Have Web Development Skill

Apache mod_rewrite is a module of Apache web server which allows server side manipulation of the URLs. (If you need help with Apache web server, read this first). This means that you can redirect http://abc.com to http://zxy.com without changing the URL in the client's address bar. All you need to know is the syntax of mod_rewrite (which I'll tell you here) and some regular expressions (though it is not mandatory). A great cheat sheet for regular expression is available from AddedBytes.com. If you want to know who uses mod_rewrite then let me tell you, whenever you hear of something known as "clean URL", most probably mod_rewrite is behind it. Wordpress, Drupal and many other blogging platforms and CMSs produce clean URLs using mod_rewrite.

To ensure that mod_rewrite is enabled on your apache, just make sure that your apache config file has the following line uncommented:
LoadModule rewrite_module modules/mod_rewrite.so

If it was commented before and you have uncommented it manually, then you need to restart your apache web server. To check if mod_rewrite has been enabled or not, just create an empty file anywhere in your web root and write the following line:
<!--?php phpinfo(); ?-->

Open this file in any web browser and check out apache2handler. mod_rewrite would appear there if it is enabled successfully.
Now let us write some basic mod_rewrite rules for real. Suppose tha you want to redirect all the traffic to you website to a fixed URL (this generally happens when you take down your site for update or maintenance). All you need to do is write the following lines in your .htaccess (more on .htaccess in some other post):-
RewriteEngine On
RewriteRule .* maintenance.php

First line will turn the Rewrite engine on (it is usually on but we like to be on the safe side). The second line will capture all the incoming URL (.* is the regular expression here) and will redirect it to the maintenance.php in the web root.

Now let us create a more useful rewrite rule. Something similar to what is used to create clean URLs. Consider a website which takes input in the form http://abc.com/name.php?q=aditya. This looks ugly and is not search engine friendly. We would like to have something like http://abc.com/name/aditya. This would be nice and easy to remember. For this let us create a rule for mod_rewrite.
RewriteEngine On
RewriteRule ^name/(\w+)/?$ name.php?id=$1

The Rewrite engine is going to examine the incoming URL requests and (time to open the regular expression cheat sheet if you do not master it) convert the friendly URL into the URL understandable by the server.

We can also create custom error messages using mod_rewrite. For creating a 404 Not Found message, follow the rules given below:

15 comments:

  1. Great. I'm a newbie web designer in perth and this helps me a lot. Thanks for sharing!

    ReplyDelete
  2. I work as an iphone app developer Perth and a freelance web developer too and I think that this is really the best seo tool out there. I am amazed for the reason that you gave a really brief explanation about it and it is easy to understand.

    ReplyDelete
  3. This code is really the best seo tool for me and I learned it from a Perth seo company. Creating custom error messages is my favorite thing to do using mod_rewrite.

    ReplyDelete
  4. Yes, apache_mod can be very handy in rewriting ugly urls to make them prettier (and when I say prettier, it only means it's search engine friendly). The seo organic optimization techniques have rules on where apache_mod will be very helpful.

    ReplyDelete
  5. This is a really must have web development skill. It is not just for SEO people but it can be useful for freelancers as well. I am still practicing and I can say that it’s really useful and it makes my job a lot easier.

    -Eden Gertrude

    ReplyDelete
  6. apache_mod makes bad URLs look more organized in SEO and web development. I wonder why only a few developers have this skill. It lessens the headache of dealing with a load of codes. -- Alex Fenston

    ReplyDelete
  7. This is really interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your magnificent post. Also, I’ve shared your site in my social networks!
    SEO Company Birmingham

    ReplyDelete
  8. If you want to know who uses mod_rewrite then let me tell you, whenever you hear of something known as "clean URL", most probably mod_ www.netmark.com rewrite is behind it. Wordpress, Drupal and many other blogging platforms and CMSs produce clean URLs using mod_rewrite.

    ReplyDelete
  9. Thanks for this post. Very interesting. SEO folks should be aware of the possible webserver performance hits modrewrite may cause. The issue of slowing down the website with too many rewrites can be found here: http://blog.preinheimer.com/index.php?/archives/340-.htaccess-to-httpd.conf.html
    The blog author also provides code work-arounds as well.
    Cheers,

    ReplyDelete
  10. Nice sharing.

    I am using Tucktail.com here they are providing many features with every plans and they are supporting with excellent customer service at 24/7.Their servers are 99% uptime.This is the safe and reliable website.

    ReplyDelete
  11. Wordpress, Drupal and many other blogging platforms and CMSs produce clean URLs using mod_rewrite. Niche Related

    ReplyDelete
  12. I am impressed very informative post and carries very useful knowledge for readers. I think agile system development is best for SEO. The modern seo is always taking on new roles and responsibilities. But there are many problems and limitations with this model for SEO in large organizations. We must do better to achieve greater results through Agile development methodology.

    ReplyDelete
  13. I am amazed for the reason that you gave a really brief explanation about it and it is easy to understand. seo exeter

    ReplyDelete
  14. Very good post. I plan to reference this in my blog and provide a link to here.
    SEO Services Singapore

    ReplyDelete