ISAPI Rewrite 3 - URL Manipulation & More


#1

ISAPI_Rewrite Version 3 is a powerful URL manipulation engine based on regular expressions, which is supported on our Windows Environment as a module for Microsoft Internet Information Services (IIS).

We have lots of experience with Isapi_Rewrite Hosting. This wiki contains some examples of how to implement Isapi_Rewrite Version 3.

In order for the ISAPI Rewrite rules for your site to work they must be placed in a file named .htaccess within the sites directory. You can have the .htaccess file placed in any directory under the site including the root directory (such as /wwwroot for our windows environment), or you can place the file under any sub-directory.

Note: If placed in a sub-directory the rewrite rules will only take affect for that specific directory and any sub-directories beneath it.

Examples of Rewrites (below):

Redirecting to a different domain

If you need to redirect your website to another website
RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [QSA,R=301]

Rewrite a Folder to another Folder

RewriteEngine on
RewriteBase /

RewriteRule ^oldfolder$ /correctfolder [NC,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 [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 %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

www. to non-www. Redirects

Redirecting www version to non-www., example www.domain.com to domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com
RewriteRule ^ http://domain.com%{REQUEST_URI} [R=301,L]

URL Rewrite

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]

WordPress Permalinks

WordPress Permalinks using mod_rewrite are for Linux, but ISAPI_Rewrite does offer the equivalent. If you want to have index.php not show in the url try using these in your .htaccess file.

If your WordPress site is in the wwwroot folder.

# BEGIN WordPress

#Options +Followsymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [NC,L]

# END WordPress

If your WordPress site is in a subfolder.

# BEGIN WordPress

#Options +Followsymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subfolder/index.php [NC,L]

# END WordPress

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

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]

HTTP to HTTPS SSL Rewrites

Suppose you have URL like http://shop.example.com and you want your visitors to be redirected to https://shop.example.com

Here are some examples of how to force SSL. Simply place the following rules into your .htaccess file:

HTTP to HTTPS Redirect

# Enable rewrite rules RewriteEngine On
## Redirect HTTP to HTTPS

# Only trigger rule if a non-ssl port is being used
RewriteCond %{SERVER_PORT} !443
# Redirect to HTTPS
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301]

HTTP to HTTPS with www Redirect

This version will be both redirected to HTTPS as well as add "www." to the beginning of the hostname if it is missing.
# Enable rewrite rules
RewriteEngine On


## Redirect HTTP to HTTPS with www

# Only trigger rule if a non-ssl port is being used
RewriteCond %{SERVER_PORT} !443
# Extract non-www portion of HTTP_HOST
RewriteCond %{HTTP_HOST} ^(www\.)?(.*) [NC]
# Redirect to HTTPS with www
RewriteRule (.*) https://www.%2/$1 [R=301]
NOTE: If you prefer to remove the www from the URL, simply change "https://www.%2/$1" to "https://%2/$1"

We recommend visiting HeliconTech for more help and guidance on ISAPI Rewrite Rules if needed.


Redirect Rules with web.config
What are Mixed Content Errors?
Common Uses for .htaccess files