ISAPI Rewrite
Contents
ISAPI_Rewrites and Redirects Version 3
ISAPI_Rewrite Version 3 is a powerful URL manipulation engine based on regular expressions. Hostek.com has lots of experience with Isapi_Rewrite Hosting. Here are a couple of examples of using Isapi_Rewrite Version 3:
NOTE: Place the rewrite rules in a file named .htaccess and place it at the web root (ie, /wwwroot folder)
Simple Redirects and Rewrites
Redirecting to a different domain
If you need to redirect your website to another website
RewriteEngine On RewriteCond %{HTTP_HOST} ^domain.com$ [NC] RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
Redirect File Names
To have your index.htm page auto redirect to index.asp user this example
RewriteEngine on RewriteRule index.htm index.asp [I,O,R=301,L]
Subfolder Rewrite
To redirect your domain to a subfolder of that domain example: www.domain.com to www.domain.com/folder
RewriteEngine On # Exclude requests already going to /subfolder to avoid an infinite loop RewriteRule ^subfolder.*$ - [NC,L] # Rewrite normal requests to /subfolder RewriteRule ^(.*)$ /subfolder/$1 [L]
non-www. to www. Redirects
Redirecting non-www version to www., example domain.com to www.domain.com
RewriteEngine on RewriteCond %{HTTPS} (on)? RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC] RewriteCond %{REQUEST_URI} (.+) RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]
Suppose you have URL like www.example.com/foo.asp?a=A&b=B&c=C and you want to access it as www.example.com/foo.asp/a/A/b/B/c/
RewriteEngine on RewriteRule ^(.*?\.asp)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3 [NC,LP,QSA
HTTP to HTTPS SSL Rewrites
SSL
Suppose you have URL like http://shop.example.com and you want your visitors to be redirected to https://shop.example.com
Here is example how to force SSL for certain folder. Simply put following rules into the .htaccess file in this folder:
RewriteEngine on #Fix missing trailing slash char on folders RewriteRule ^([^.?]+[^.?/])$ $1/ [R,L] #Redirect non-HTTPS to HTTPS RewriteCond %{HTTP:Host} (.*) RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} (.*) RewriteRule .? https://%1%2 [R,L]
RewriteEngine on # handle non-www HTTPS redirects. Consecutive conditions are implicitly ANDed together. RewriteCond %{SERVER_PORT} ^443$ RewriteCond %{HTTP_HOST} ^(?!www\.).*mywebsite\.com$ RedirectRule ^/(.*)$ https://secure#.ezhostingserver.com/mywebsite-com/$1 [R=301] # All requests arriving to this point either use www for the hostname, or use # HTTP for the protocol. # handle non-www non-HTTPS redirects RewriteCond %{HTTP_HOST} ^(?!www\.).*mywebsite\.com$ RedirectRule ^/(.*)$ http://www.mywebsite.com/$1 [R=301]
Site Crawlers
Example on how to prevent certain spiders from crawling your site.
RewriteEngine on RewriteCond %{HTTP_USER_AGENT} ^Baiduspider.*$ RewriteRule .* /block.htm RewriteCond %{HTTP_USER_AGENT} ^Yandex.*$ RewriteRule .* /block.htm
Wile-Card Subdomains & Variables Rewrites
Here is an example to show how to get the variables from positions 1 and 2 without it mattering how many items are in the URL. In other words, a good example for a rewrite rule for optional parameters.
Variable URLs
Let's say you want to have a URL display like: http://your_domain.com/some-folder/34-77-some-key-word.html But you want that to really process a query like:http://your_domain.com/folder/search.asp?country=34&city=77
RewriteEngine on RewriteRule ^some-folder/([^-]+)-([^-]+)-.*$ /folder/search.asp?country=$1&city=$2
Wild-Card Subdomains
Rewrite all wild-card sub-domain requests to a folder without affecting "your_domain.com" or "www.your_domain.com"
# Ignore requests that are already rewritten RewriteRule ^subdomainfolder/.*$ - [NC,L] # Rewrite all requests to non-www sub-domains to /subdomainfolder RewriteCond %{HTTP_HOST} !^(www\.)?your_domain\.com$ [NC] RewriteRule ^(.*)$ /subdomainfolder/$1 [L]
For more Examples and other uses please visit Helicon Tech