I’ve been using ISAPI Rewrite for the last three years to improve the quality of web site URLs. Its become such an important of my web work that its one of the first things I tell my clients to add to their server toolkit. Infact, I continue to be amazed (and frustrated) that Microsoft has not included a powerful URL rewriter tool with every copy of Internet Information Server.
Now, Ive seen lots of examples of using URL rewriters to hide query string arguments from search engines. Ive also seen a few examples of how to use URL rewriters to route users to special sites based on language or browser types. But there are several other important reasons to be using URL rewriters in your web deployments.
Here are a couple of rewriter rules that I add to almost every public web site I am involved in.
Putting the WWW back in the World Wide Web
Most public web sites respond to both http://www.mydomain.com and mydomain.com. Ive gotten into the habit of *not* typing the www. since I know the plain address will work just fine. It means the same to me. However,it turns out that most search engines (Google, Yahoo!, MSN, etc.) do not treat these two addresses the same. Most engines will track links and indexes to both locations. If youre focusing on your web sites ranking with these search indexes, having both addresses can be a problem. To fix this, I use a simple rewriter rule to automatically re-route browsers that present mydomain.com to http://www.mydomain.com. This allows lazy users (like me) to keep typing the short name, but still get the full name in reply.
Heres the rule I use with ISAPI Rewrite:
# force proper subdomain on all requests
RewriteCond %HTTP_HOST ^mydomain.com
RewriteRule ^/(.*) http://www.mydomain.com/$1 [RP,L]
Now, users will always see the full name. And keep in mind that some of the most important users of your web site are search engine spider bots!
The ZIPmouse Internet Directory is one of the companies Ive worked with that has this rule in their rewriter set. Try typing http://zipmouse.com in your browser and see what happens.
Fixing the Missing Slash
One annoying problem with web sites is how they handle missing slashes in URLs. For example, look at this address:
http://www.mydomain.com/members
Usually, what users want is the default page in the members folder of the site. They just forgot about the trailing slash. I use the following rewrite rule to automatically add the trailing slash:
# fix missing slash on folders
RewriteCond Host: (.*)
RewriteRule ([^.?]+[^.?/]) http://$1$2/ [I,R]
Heres another example from the ZIPmouse site: http://www.zipmouse.com/city/seattle
Dropping the Default
Finally, heres a rule I really like to use for public sites. Ill present it first to give you a chance to think about it.
RewriteRule (.*)/default.html $1/ [I,RP,L]
We all know that typing just a folder will force the web server to return the default document. This rule does the opposite. It checks the URL for the registered default page for the site and strips the URL down to only the folder. A good rewriter file would probably do this for all the typical defaults registered on the server:
RewriteRule (.*)/default.htm $1/ [I,RP,L]
RewriteRule (.*)/default.asp $1/ [I,RP,L]
RewriteRule (.*)/default.aspx $1/ [I,RP,L]
RewriteRule (.*)/index.htm $1/ [I,RP,L]
The ZIPmouse directory uses home.html as the default page for their site. You can use the following URL to test the above rule.
http://www.zipmouse.com/shop/computers-and-internet/home.html
Summary
So there are three handy URL rewrite rules that can improve the look and feel of your web sites URLs. If you are not using a URL rewriter yet, I encourage you do start. You can download a free for non-commercial version of ISAPI Rewriter for Microsoft server from their website. There are other rewriters out there, too.
Technorati Tags
I tag my posts for easy indexing at Technorati.com