Difference between revisions of "ISAPI Rewrite"

From Hostek.com Wiki
Jump to: navigation, search
(Simple Redirects and Rewrites)
Line 5: Line 5:
 
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:
 
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:
  
<b>NOTE:</b> Place the rewrite rules in a file named .htaccess and place it at the web root (ie, /wwwroot folder)<br />If you do not have a .htaccess file created already then use a text editor like Notpad and save the file as a .htaccess or use the File Manager in the hosting control panel to create the .htaccess file on the server.
+
*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 [https://wiki.hostek.com/Mod_rewrite Mod_Rewrite Wiki] and our [https://wiki.hostek.com/URL_Rewrite#Apache_.28Linux.29 URL Rewrite Wiki.]
  
 +
<pre>
 +
IMPORTANT: Place the rewrite rules in a file named .htaccess and place it in the website's Default Directory(i.e. the \wwwroot folder).
 +
</pre>
 +
*If you do not have an .htaccess file already created you can:
 +
*#Open a text editor--like Notepad--and save the file as ".htaccess".
 +
*#Use the File Manager in the hosting control panel to create a .htaccess file on the server.
  
 
== Simple Redirects and Rewrites ==
 
== Simple Redirects and Rewrites ==

Revision as of 22:06, 18 January 2017


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:

  • 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

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
RewriteEngine on

RewriteCond %{SERVER_PORT} !443
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)$
RewriteRule ^(.*)$ https://%2/$1 [R,L]

Non-www, non-HTTPS to www, HTTPS redirects

RewriteEngine on
RewriteCond %{SERVER_PORT} !443
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} ^on(s)|
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R,L]

Shared SSL

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


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