301 redirecting on a Windows server ( same as mod_rewrite / .htaccess )
It’s not news at all that LAMP ( Linux, Apach, MySQL, PHP ) servers handle many things via mod_rewrite and .htaccess.
Frankly, we’ve been jealous of what can be done so simply on a LAMP server via a .htaccess file that is just NOT so simple at all to do on a Windows server via IIS.
However, we’re never daunted . . . there is always a way!
Querying this subject via search engines will likely leave on with the impression that it just can’t be done as easily on a Windows/ISS server as it can be done on a LAMP server . . . . but the brilliant minds at HeliconTech.com have cracked the nut! And WMS is righgt behind them.
HeliconTech’s most recent version of ISAPI_Rewrite ( version 3 ) emulates pretty much everything that can be done via mod_rewrite and .htaccess files on a server and site ( respectively ), save for a very small set of advanced options that might make sense on a LAMP server, but just don’t make any sense on a Windows/IIS server. In fact, it is so compatible that if one were to migrate from a LAMP server to a Win/IIS server, one should be able to migrate the .htaccess file along with it and be off an running ( assuming the Win/IIS server is runnig ISAPI_Rewrite ).
WMS looked into this issue for two principal reasons:
- We needed a way to rewrite all non-www domain calls to www domain calls.
- We needed a simple way for clients to 301 redirect pages on a site rebuild where some, many, most, or even all of their pages changed names because of the rebuild.
To be sure, proper 301 redirects from, say, domainA.com to domainB.com are simple to do in IIS. However, 301 redirecting a non-www call to a www call is not so simple — simply because it is typical with both manual IIS edits and via hosting control panels ( we use HELM 3 currently ) to set up the site in IIS as “domain.com” that then use headers and DNS to direct www.domain.com to the “domain.com” location in IIS/DNS. That is, as “domain.com” is the identity of the website in IIS, there is no easy way to redirect that back to “www.domain.com” through IIS.
Honestly, to manage this sort of thing gracefully requires the sort of functionality mod_rewrite and .htaccess files provide on a LAMP server.
What is more, there just is no simple way to do page for page 301 redirects via IIS either — say 301 redirect pageA.php to pageA.aspx, for example. This is handled very nicely via mod_rewrite on a LAMP server, but not well handled at all on a Windows/IIS server . . . . Until ISAPI_Rewrite version 3!
Installing ISAPI_Rewrite solves all of our issues here.
However, just as soon as we got all excited about that, we immediately ran into issues . . . go figure.
For example, you may already have an ISAPI that is currently using .htaccess ( such as IIS Password ), so ISAPI_Rewrite may throw a conflict with that. In our case we experienced a conflict with IIS Password, which we already had plugged into HELM.
This is easily handled, though! ISAPI_Rewrite enables you to explicitely define what file you would like to use to control the site-level rewriting equivanlent to the .htaccess file one would use on a LAMP server. We opted to “roll back” to the httpd.ini used by prior versions of ISAPI_Rewrite.
So, if you are trying to 301 redirect non-www URLs to their www URL and/or trying to find a solution to 301 redirect legacy pages on a rebuilt site to their new locations on a Windows server, then Helicon’s ISAPI_Rewrite version 3 is your salvation.
To globally redirect all non-www calls to your domains, use the following code in your C:\Program Files\Helicon\ISAPI_Rewrite3\httpd.conf file:
AccessFileName httpd.ini
To 301 redirect pages
RewriteEngine on
# Redirect non-www to www, except when calling by IP or subdomain:
RewriteCond %{HTTP:Host} ^(?!65\.98\.245\..*).*$
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)([^.]+\.[^.]+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]
To 301 redirect pages from a legacy site to a new production site, use the following rules in the sites webroot httpd.ini file:
RewriteEngine on
RewriteRule ^index\.html$ default.aspx [NC,R=301]
December 12th, 2008 at 6:55 pm
[...] BTW, this has the added benefit of assuring that the domain is not accessed without http://www. — another SEO benefit. However, we recommend that you handle this particular issue globally. See 301 Redirecting on a Windows Server. [...]
June 17th, 2009 at 11:16 am
What if I simply want to redirect:
http://www.domain.com/abc.html
to open a page:
http://www.domain.com/efg.html
(and I have hundreds of random page name pairs like that)
How do I define a single pair abc.html to open efg.html?
June 17th, 2009 at 11:46 am
Ivan,
That’s addressed in the last line of the post. You would simply use:
RewriteRule ^abc\.html$ efg.html [NC,R=301]
August 24th, 2009 at 4:31 am
Thanks for the article. Need to complete a large 301 redirect for one of my clients and your article was a very good refresher.
December 4th, 2009 at 10:21 am
I have a client with almost 500 redirects of random names the same as Ivan posted. We tried using the RewriteRule in ISAPI ReWrite as coded above and at around 150 rules the performance of the site was unacceptably poor.
It’s the only site on the server.
Anyone know of anything that can handle a large number of redirects efficiently?