1. Introduction
IIS Mod-Rewrite, Pro Edition, from
Micronovae is
a URL rewrite engine that emulates, on IIS web servers, the same
functionality as Apache mod_rewrite, a popular tool to manipulate the
way URLs are displayed on a website, or to redirect a URL to a
particular web page or directory.
Rewrite rules are placed within a .htaccess file (which is then placed
in the root of your site or directory) to instruct the rewrite engine
how to respond when a specified URL is entered into a web browser that
needs to be changed or redirected.
This tool can be used to redirect an additional domain (which by default points to the root of your website just as your primary domain would) to point to a sub-directory instead. You can then rewrite the URL and have your additional domain appear as if it were hosted in its own, separate root-level website.
For example: you have created two websites, one of which is located in a sub-directory.
www.apple.com
www.apple.com/banana
You, however, own www.banana.com but don't want to host it on its own, separate hosting plan.
With the applicable rewrite rules in your .htaccess file, you would be able to redirect the URL "
www.banana.com" to "
www.apple.com/banana," and at the same time hide the apple.com/banana domain and sub-directory so it appears just as "www.banana.com."
2. Creating a .htaccess file to accomplish this.
IIS Mod-Rewrite is a rules-based rewriting engine based on a
regular-expression parser, as is the more familiar Apache mod_rewrite
module. In fact, IIS Mod-Rewrite is 100% compatible with Apache
mod_rewrite, implementing all its features. Rewrite configurations can
even be migrated from IIS to Apache, and vice versa, by just doing a
copy.
If you are familiar with regular expressions and interested in writing
your own rewrite rules, you can locate a good regex cheat sheet
here.
We have, however, created some examples that you could use to quickly and easily host one or more additional domains as their own websites under a single hosting plan. Although there are many
different ways to write rewrite rules for a desired outcome, the below
examples have been successfully tested in our hosting environment.
*Click
here to see if your hosting plan supports IIS Mod-Rewrite.
Redirecting a secondary domain to a directory
Copy and past the following into your preferred text editor. Save
the file as ".htaccess" (without quotations) and place it in the root
of your site. If your text editor appends a ".txt" or ".html," be sure
to remove it. Also be sure to replace the example domains with your domains.
In the below example "www.banana.com" is your secondary domain and "directory/" in lines 3 and 4 is the name of the directory where it is located.
#
RewriteEngine On
RewriteCond %{HTTP_HOST} banana.com
RewriteCond %{REQUEST_URI} !directory/
RewriteRule ^(.*)$ directory/$1 [L]
#
Several domains under a single hosting plan
#
RewriteEngine On
RewriteCond %{HTTP_HOST} banana.com
RewriteCond %{REQUEST_URI} !directory1/
RewriteRule ^(.*)$ directory1/$1 [L]
#
RewriteCond %{HTTP_HOST} orange.com
RewriteCond %{REQUEST_URI} !directory2/
RewriteRule ^(.*)$ directory2/$1 [L]
#
RewriteCond %{HTTP_HOST} grapefruit.com
RewriteCond %{REQUEST_URI} !directory3/
RewriteRule ^(.*)$ directory3/$1 [L]
#