Note! All rules in these examples are intended for httpd.conf file. In ISAPI_Rewrite as well as in Apache mod_rewrite base path for rules is different depending on a directory where you put .htaccess file. Initial leading slash only exist if you put rules in httpd.conf, while in .htaccess files virtual path to these files is truncated. Rules that rely on a root path are preceded with RewriteBase / directive to allow them to work in any location within httpd.conf and directory level .htaccess files.
This example demonstrates how to easily hide query string parameters using loop flag. Suppose you have URL like http://www.mysite.com/foo.asp?a=A&b=B&c=C and you want to access it as http://www.myhost.com/foo.asp/a/A/b/B/c/C
Try the following rule to achieve desired result:
Note that this rule may break page-relative links to CSSs, images, etc. This is happening due to a change of the base path (parent folder of the page) that is being used by a browser to calculate complete resource URI. This problem occurs only if you use directory separator as a replacement character. There are three possible solutions:
There also exist many variations of this rule with different separator characters and file extensions. For example, to use URLs like http://www.myhost.com/foo.asp~a~A~b~B~c~C the following rule can be implemented:
Keyword rich URLs
In previous example we have used general technique to simply hide query string markers. But much more useful solution for search engine optimization would be making your URL keyword rich. Consider following URL example: http://www.mysite.com/productpage.asp?productID=127 This is very usual situation for most web sites. But you can significantly increase rating of your page in search engines by using the following URL format instead: http://www.mysite.com/products/our_super_tool.asp Keywords “our super tool” in this URL will be indexed and improve page rank. But “our_super_tool” cannot be used to retract productID=127 directly. Several solutions to this problem exist.
The first solution that we would recommend if you have short URL format with only few parameters is to include in URL both keywords and numeric identifiers. In this case your URL may look as: http://www.mysite.com/products/our_super_tool_127.asp Only one rule will be needed to achieve this rewrite:
Another and more complex solution is to create 1 to 1 map file and use it to map “our_super_tool” to 127. This solution is useful for some long URLs with many parameters and will allow you to hide even numeric identifier. The URL will look as http://www.mysite.com/products/our_super_tool.asp. Please note that “our_super_tool” part should uniquely identify the product and it’s identifier. Here is an example for this solution:
And you will need to create mapfile.txt map file with the following content:
Advantage of this method is that you can use it to combine quite complex URL transformations, but this is a bit out of this small examples guide context.
Assume you have internet server running IIS and several backend servers or applications running other platform or machine. These servers are not directly accessible from the internet but you need to provide access to these servers for others. Here is an example of how to simply map entire content of one web site into the folder on another site running ISAPI_Rewrite:
While it is not possible to completely hide all file extensions on the site we can use ISAPI_Rewrite's file checking feature to hide some known extensions. Here is a rule example to hide .asp file extensions on the site:
Emulating host-header-based virtual sites
For example you have registered two domains www.site1.com and www.site2.com. Now you can create two different sites using single physical site. Here is a rules example:
Now just place your sites in /site1 and /site2 directories. Note that www.site1.com and www.site2.com should be somehow mapped in IIS to this web site to allow ISAPI_Rewrite intercept request.
Or you can use more generic rules to map any request to the folder with the
same name as the host name in request:
Directory names for sites should be like /somesite1.com, /somesite2.info, etc.
Assume you have some pages with inline GIF graphics under http://www.mysite.com/. Some other sites incorporate this graphics via hyperlinks to their pages. This adds useless traffic to your site and you want to stop this practice.
While you cannot 100% protect the images from inclusion with ISAPI_Rewrite, only HotlinkBlocker product can do this, you can at least restrict the cases when browser sends HTTP Referer header. The following rules will allow only access to the images if referer is from the same host or empty.
Redirecting non-www version to www
If your web site is accessible through both versions of URL, i.e. http://helicontech.com and http://www.helicontech.com it is a good idea to redirect one version to another to avoid possible duplicate content penalty from search engines. Here is a rule to generally 301 redirect everything non-www to the corresponding www resource.
Force to SSL or non-SSL for certain locations
Here is example how to force SSL for certain folder. Simply put following rules into the .htaccess file in this folder:
And here is example how to do the contrary - force non-SSL for certain folder.
Moving site location
This is very usual problem when you move web site from one domain name to another, or just another folder. You want to redirect all requests from one web site to another preserving the requested resource name and parameters. This is incredibly useful especially when you want to preserve page ranks of existing pages and external links. The solution is to use ISAPI_Rewrite on the old web server:
It is sometimes necessary to provide browser-dependent content at least for important top-level pages, i.e. one has to provide a full-featured version for the Internet Explorer, a minimum-featured version for the Lynx browsers and an average-featured version for all others.
Block annoying robots
Here is a useful example to block a number of known robots and retractors by their user agents. Please note this rule is long and we have divided it into lines. In order to work correctly no spaces can be added to the end or beginning of the lines:
Dynamically generated robots.txt
robots.txt is a file that search engines use to discover URLs that should or should not be indexed. But creation of this file for large sites with lots of dynamic content can be a very complex task. Have you ever thought about robots.txt dynamically generated from a script? Let's write robots.asp script:
Now make it available as robots.txt using single rule:
This example emulates some kind of DNS Round-Robin load balancing technique. Suppose you have main site www.mysite.com and a number of web servers which you have registered as www[1-9].mysite.com If you install ISAPI_Rewrite on the main server, you can spread traffic randomly between all servers by redirecting initial client request to some specific server. Once redirected, client will continue using this specific server. While this solution is not ideal, it can really spread your traffic and help to avoid problem with preserving session state.
Use the following rule to redirect clients:
And here is hosts.txt file content:
Please find many other useful examples in Apache documentation here. Most of them can be used with ISAPI_Rewrite 3 without any modification.