Difference between revisions of "ISAPI Rewrite"

From Hostek.com Wiki
Jump to: navigation, search
(HTTP to HTTPS SSL Rewrites)
(HTTP to HTTPS with www Redirect)
 
Line 216: Line 216:
 
RewriteCond %{SERVER_PORT} !443
 
RewriteCond %{SERVER_PORT} !443
 
# Extract non-www portion of HTTP_HOST
 
# Extract non-www portion of HTTP_HOST
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)
+
RewriteCond %{HTTP_HOST} ^(www\.)?(.*) [NC]
 
# Redirect to HTTPS with www
 
# Redirect to HTTPS with www
 
RewriteRule (.*) https://www.%2/$1 [R=301]
 
RewriteRule (.*) https://www.%2/$1 [R=301]
Line 222: Line 222:
  
 
''NOTE:  If you prefer to remove the www from the URL, simply change "<nowiki>https://www.%2/$1</nowiki>" to "<nowiki>https://%2/$1</nowiki>"''
 
''NOTE:  If you prefer to remove the www from the URL, simply change "<nowiki>https://www.%2/$1</nowiki>" to "<nowiki>https://%2/$1</nowiki>"''
 
  
 
==== Shared SSL ====
 
==== Shared SSL ====

Latest revision as of 18:47, 9 February 2017


ISAPI_Rewrites and Redirects Version 3

ISAPI_Rewrite Version 3 is a powerful URL manipulation engine based on regular expressions, which is supported on our Windows Servers. Hostek.com has lots of experience with Isapi_Rewrite Hosting. This wiki contains some examples of how to implement Isapi_Rewrite Version 3.

  • ISAPI_Rewrite is an IIS Module that is very similar to the Apache Module "Mod_Rewrite", Mod_Rewrite is also enabled on our LiteSpeed servers.
  • For more information on Mod_Rewrite please see our Mod_Rewrite Wiki and our URL Rewrite Wiki.
IMPORTANT: Place the rewrite rules in a file named .htaccess and place it in the website's Default Directory(i.e. the \wwwroot folder).
  • If you do not have an .htaccess file already created you can:
    1. Open a text editor--like Notepad--and save the file as ".htaccess".
    2. Use the File Manager in the hosting control panel to create a .htaccess file on the server.

Simple Redirects and Rewrites

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

Redirect wwwroot/public_html to subfolder

Allows you load your domain from a subfolder instead of the wwwroot/public_html folder.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subfolder/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /subfolder/$1 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subfolder/index.html [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 both redirect 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"

Shared SSL

If you are using the shared SSL and want to force SSL, the below script will redirect traffic to the shared SSL URL:

# Enable rewrite rules
RewriteEngine On


## Redirect HTTP to HTTPS on shared SSL

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

NOTE: Replace the above URL with the correct shared SSL URL for your site, which can be found within your hosting control panel.

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


Wild-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