Recent Comments

Tags

Widgets



Comodo SSL Seal

Using ISAPI_Rewrite For SEO Friendly URLS

Author Asif in IIS on Jun 14 2008 5:17PM

Tags: Tags IIS Tags isapi Tags RewriteRule Tags regex

ISAPI_Rewrite is sort of like mod_rewrite, so if your familiar with it and comfortable with regular expression then you should have no problem setting this up. Normally, In Apache, you would use .htaccess files to enable mod_rewrite and write your regular expressions, but with IIS .htaccess is not supported thus you would use httpd.ini file in your root directory.
 
If you don't manage your IIS or if your hosting provider doesn't support ISAPI_Rewrite, then you might want to look up  http://cheeso.members.winisp.net/IIRF.aspx or http://www.urlrewriter.net. Thankfully, my web host, LFC Hosting, supports ISAPI_Rewrite.
 

Here are a few examples
---------------------------------------------------------------------
In your Httpd.ini file:
 
[ISAPI_Rewrite]
 
# Block external access to the httpd.ini and httpd.parse.errors files
RewriteRule /httpd(?:\.ini¦\.parse\.errors) / [F,I,O]

# Block external access to the Helper ISAPI Extension
RewriteRule .*\.isrwhlp / [F,I,O]

RewriteRule .*(?:global.asa|default\.ida|root\.exe|\.\.).* . [F,I,O]
 
#force trailing slash
RewriteCond  Host:  (.*)
RewriteRule  ([^.?]+[^.?/]) http\://$1$2/ [I,R]

#Hide file extension
RewriteCond %{REQUEST_FILENAME}.aspx -f
RewriteRule (.*) $1.aspx
 
#force https on a directory
RewriteCond %HTTPS (?!on).*
RewriteRule /DIRECTORY(/.*)? https\://www.site.com/DIRECTORY$1 [R,L]
 
#Rewrite querystring for a particular page using directory rewrite
RewriteRule .*page/(.*)/(.*)/(.*) /page.aspx?var1=$1&var2=$2&var3=$3 [I]
// I use this rule on this site as you can see from the url

Of course, the directory rewrite will break page-relative links to images, CSS, etc. You can either implement a non-directory rewrite, change all page-relative links to root-relative or absolute, or you can do what I do and use the <base href=""> tag. I use the that tag in my site.master and it has been easy for me.

 
More rewrites......
 
#Rewrite non-specific page querystring
#http://www.mysite.com/foo.asp?a=A&b=B&c=C ==>
#http://www.myhost.com/foo.asp/a/A/b/B/c/C
RewriteRule ^(.*?\.aspx)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3 [NC,LP,QSA]

#If you wanted to do http://www.myhost.com/foo.asp~a~A~b~B~c~C
RewriteRule ^(.*?\.asp)~([^~]*)~([^~]*)(.*) $1$4?$2=$3 [NC,LP,QSA]

#Stop image hot-linking
RewriteCond %{HTTP:Host}#%{HTTP:Referer} ^([^#]+)#(?!http://\1).+
RewriteRule .*\.(?:gif|jpg|png) /block.gif [NC]

#Browser dependant content
RewriteCond %{HTTP:User-Agent} MSIE
RewriteRule /foo\.htm /foo.IE.htm [L]

RewriteCond %{HTTP:User-Agent} (?:Lynx|Mozilla/[12])
RewriteRule /foo\.htm /foo.20.htm [L]
RewriteRule /foo\.htm /foo.32.htm [L]
 
 
More information on ISAPI_Rewrite can be found here:
http://www.isapirewrite.com


 

Leave a Comment

No Comments Yet