Difference between revisions of "ISAPI Rewrite"

From Hostek.com Wiki
Jump to: navigation, search
(Redirecting to a different domain)
m
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)
+
<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.
  
  

Revision as of 18:42, 15 July 2013


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)
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.


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 [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]

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

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]

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