ISAPI Rewrite

Since Thursday, 11 March 2010, ISAPI_Rewrite version 3 has been standardly available in our hosting environment.

Please Note: Only version 3 is installed on our Windows Server 2008 web servers.

ISAPI_Rewrite version 3 is compatible with Apache mod_rewrite syntax, which makes moving a configuration from Apache to IIS as simple as copying the .htaccess file.

 


You’ll find more information in the online ISAPI Rewrite 3 online documentation.

An operating system like Microsoft Windows does not allow you to save files beginning with a dot (‘.’). If you make a .htaccess file, create it as htaccess.txt. Upload the file via FTP and use your FTP program to rename the file to .htaccess.


Linked Domains

You can use the .htaccess code below to link domains registered using “Domain name registration without hosting” to your main domain:

 # Schakel de RewriteEngine in.
 RewriteEngine on
 
 # In iedere onderliggende map kan een .htaccess-bestand de configuratie
 # overschrijven.
 AllowOverride all
 
 # Stuur het HTTP-request voor ieder gekoppeld domein door naar het
 # hoofddomein. Vervang "mijnhoofddomein" op de eerste en derde regel
 # hieronder door de domeinnaam van het hoofddomein.
 RewriteCond %{HTTP:Host} ^(?!www.mijnhoofddomein.nl).*$ [NC]
 RewriteCond %{REQUEST_URI} (.+)
 RewriteRule .? http://www.mijnhoofddomein.nl%1 [L,R=301]

A 301-HTTP redirect forwards calls to http://www.gekoppelddomein.nl/map/pagina1.html on to http://mijnhoofddomein.nl/map/pagina1.html.

Or to have a linked domain come out in a subfolder without a redirect:

 # Laat een gekoppeld domein uitkomen in een sub-map, met de naam van
 # dat domein.
 #
 # Schakel de RewriteEngine in.
 RewriteEngine on
 
 # In iedere onderliggende map kan een .htaccess-bestand de configuratie
 # overschrijven.
 AllowOverride all
 
 # Wijzig "gekoppelddomein" in de domeinnaam, op de eerste en derde regel
 # hieronder. Wijzig "tld" in de extensie van het domein (nl, com, net, eu).
 RewriteCond %{HTTP:Host} ^(?:www.)?gekoppelddomein.tld$
 RewriteCond %{REQUEST_URI} (.+)
 RewriteRule .? /gekoppelddomein%1 [L]

Forward an HTTP Request to HTTPS

 # Schakel de RewriteEngine in.
 RewriteEngine on
 
 # In iedere onderliggende map kan een .htaccess de configuratie overschrijven.
 AllowOverride all
 
 # Stuur ieder HTTP-request voor een specifieke host door naar HTTPS.
 RewriteCond %{HTTP:Host} ^(www.example.com)$ [NC]
 RewriteCond %{HTTPS} off
 RewriteCond %{REQUEST_URI} (.+)
 RewriteRule .? https://%1%2 [R=302,L]

 

Subdomains

 # Schakel de RewriteEngine in.
 RewriteEngine on
 
 # In iedere onderliggende map kan een .htaccess de configuratie overschrijven.
 AllowOverride all
 
 # Stuur ieder subdomein door naar een map met dezelfde naam,
 # behalve voor het subdomein "www". Er gebeurt niets als er geen subdomein is
 # gebruikt. Subdomeinen als "www.foo" worden doorgestuurd naar "/foo" en
 # "foo.bar" naar "/foo.bar".
 RewriteCond %{HTTP:Host} ^((?!www.)(.+)|(?:www.)(.+)).(example.com)$ [NC]
 RewriteCond %{REQUEST_URI} (.+)
 RewriteRule .? /%2%3%5 [L]


Domain without www to www

 # Schakel de RewriteEngine in.
 RewriteEngine on
 
 # In iedere onderliggende map kan een .htaccess de configuratie overschrijven.
 AllowOverride all
 
 # Stuur ieder HTTP-request voor een domeinnaam zonder www door naar
 # www.domeinnaam.nl.
 RewriteCond %{HTTP_HOST} ^(?!www.)(.+)$ [NC]
 RewriteCond %{REQUEST_URI} (.+)
 RewriteRule .? http://www.%1%2 [L,R=301]

 

Block all visitors, except for certain IP addresses

 # Schakel de RewriteEngine in.
 RewriteEngine on
 
 # In iedere onderliggende map kan een .htaccess de configuratie overschrijven.
 # AllowOverride all
 
 # Weiger de toegang voor iedereen, behalve de onderstaande IP-adressen.
 # Verander 11.222.33.444 en 555.66.7.888 met de desbetreffende IP-adressen.
 #
 # Dit .htaccess-bestand kan ook in afzonderlijke submappen gebruikt worden.
 RewriteCond %{REMOTE_ADDR} !(11.222.33.444|555.66.7.888)
 RewriteRule .* / [F,L]

 

ISAPI_Rewrite and REQUEST_URI

The IIS web server does not come standard with server variable REQUEST_URI. The information contained in this server variable is often needed to rewrite nice, search engine friendly URLs. For instance: WordPress Permalinks.

Helicon’s ISAPI_Rewrite 2 offered an option to use this in scripts by means of server variable HTTP_X_REWRITE_URL. This contains the same information that REQUEST_URI would contain. That is to say: the complete URI of a request. In scripts, these can be linked together with the code:

 <?php
 if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
 $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];
 ?>

so that scripts do not need any further editing, give REQUEST_URI the value of HTTP_X_REWRITE_URL once. This way it will not need to be edited dozens of time in different blocks of code.

Since Microsoft has made the ISAPI extension FastCGI available, IIS also comes standard with the real REQUEST_URI server variable. As a result, the above code change is no longer possible. Scripts, such as PHP, can now standardly use:

 $_SERVER['REQUEST_URI'];

 

The VEVIDA Windows Server 2003 web servers run ISAPI_Rewrite versions 2 and 3 alongside one another. This has been found to create conflicts in some cases. Cases of conflict:

  1. If the code uses HTTP_X_REWRITE_URL
  2. With file httpd.ini from ISAPI_Rewrite version 2

 

The recommendation (solution) is to:

  1. Undo the code edit described above. After this, the available REQUEST_URI will be used.
  2. Convert file httpd.ini to .htaccess for ISAPI_Rewrite version 3.


Temporarily shut website down for maintenance

using PHP and ISAPI_Rewrite version 3.

It is simple and easy to temporarily close your website to all visitors during site maintenance. With a friendly notice.

If your website was written entirely in ASP.NET, then you can use the well known App_Offline.htm file. This switches off the website’s pool for as long as the file is present. If you delete or rename the file, the application pool (and thus also the website) will be restarted.

An App_Offline.htm file can be placed in each application folder.

In order to achieve a similar effect without ASP.NET, you can use ISAPI_Rewrites .htaccess and PHP, for instance. The following example shuts down the website to visits from your IP address only. As a test.

Change 111.222.333.444 to your IP address.

 RewriteEngine On
 RewriteBase /
 # The next code line is currently commented out on purpose so it will be ignored.
 # It is only for testing the code when you don't really want to close the site.
 # It closes the site ONLY to you, but leaves it OPEN to everybody else.
 # When commented out, the site is closed to everyone, including yourself.
 # To enable the line, remove the # (pound sign) at the start of it.
 # You MUST first change the digits 111 222 333 444 to match YOUR IP address.
 RewriteCond %{REMOTE_ADDR} ^111.222.333.444$
 
 # It is easy to modify the above line so it does the opposite:
 # close the site to everybody else, but leave it OPEN to only you.
 # Put an exclamation point ! in front of the ^ character, to make it: !^
 # The remaining two code lines close the site. They say:
 # If the request is NOT for /maintenance.php, send /maintenance.php instead.
 # Allowing maintenance.php to be served as-is prevents an endless redirect loop.
 RewriteCond %{REQUEST_URI} !^/maintenance.php$
 
 # To allow another file, copy the line above to here and change the filename.
 # This line says: no matter what file was requested, serve maintenance.php.
 # This is a rewrite (not a redirect), so we use the local file path, no http://
 RewriteRule ^(.*)$ /maintenance.php [L]

Here, make a PHP file called maintenance.php:

 <?php
 header('HTTP/1.1 503 Service Temporarily Unavailable',true,503);
 header('Status: 503 Service Temporarily Unavailable');
 header('Retry-After: 172800');
 ?>
 
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Language" content="en-us">
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <meta content="noindex,nofollow">
 <title>503 - Temporarily Closed For Maintenance</title>
 <style>
 <!--
 p {
 font-family: "Verdana", sans-serif;
 }
 -->
 </style>
 
 </head>
 <body>
 
 <b>Name of website</b>
 
 is temporarily closed for maintenance.
 
 Normal operation will resume as soon as possible.
 
 </body>
 </html>

Upload both files to the root of your website.

Back