Papasoft

Rick Tuttle - Miami Web Application Developer

  • About
  • Wapuu Bot
  • Blog
  • Contact

How to Do a 301 Redirect on IIS Using ISAPI Rewrite

14 May 12 by Rick Tuttle

I recently launched a new WordPress site running on IIS (yes, you read correctly). The web host had ISAP Rewrite installed so we planned to use that to support Apache mod_rewrite rules.

I had used the ISAPI Rewrite filter before to handle URL rewriting like the rewrite rules that come with WordPress to support permalinks. But I had never done a 301 Redirect on IIS with this method. How hard could it be? I was replacing an ASP.NET site and needed to redirect old links like this:

OLD:  http://www.example.com/AboutUs.aspx
NEW:  http://www.example.com/about-us/

In Apache you would simply do a 301 redirect by adding the following line to .htaccess:

# This will not work using ISAPI Rewrite
Redirect 301 /AboutUs.aspx http://www.example.com/about-us/

I assumed the same syntax would work in .htaccess using ISAPI Rewrite. WRONG!

It turns out that the Redirect directive is part of Apache mod_alias and not part of mod_rewrite. So, how do you do a 301 redirect using mod_rewrite? You can do all sorts of crazy URL rewriting using mod_rewrite but I just wanted to do a simple page to page redirect!

Well, here’s how I eventually got the redirect for the About Us page and the default.aspx page to work:

RewriteRule ^default\.aspx / [R=301,L]
RewriteRule ^AboutUs\.aspx /about-us/ [R=301,L]

I hope this helps if you are trying to do a 301 redirect using ISAPI Rewrite.

Filed Under: Web Development

  • Facebook
  • LinkedIn
  • RSS
  • Twitter
  • YouTube

Comments

  1. Ben Lewis says

    10 Jul 12 at 11:17 am

    Thanks for this – been searching an answer to this issue, yours is the clearest, most concise answer I’ve found, and whats even better, it works!

    • Rick Tuttle says

      10 Jul 12 at 12:13 pm

      My pleasure Ben. Please let me know if you learn anything new.

      rt

  2. John Macclain says

    29 Jul 12 at 9:11 am

    How I redirect
    http://domain.com/;
    http://www.domain.com/index.html;
    http://domain.com/index.html – TO http://www.domain.com/

    using ISAPI Rewrite?

    • Rick Tuttle says

      29 Jul 12 at 10:37 am

      I’m not sure how you handle the subdomain rewrite. Maybe through DNS. You might try the ISAPI Rewrite forum here:

      http://www.isapirewrite.com/forum/

  3. Soan says

    8 Sep 12 at 1:15 am

    Any idea about what could be the redirect rule for removing a folder name from the URL.
    E.g.
    Redirect http://am22tech.com/Blogs/….
    to http://am22tech.com/….

    I.e. i want to remove the name Blogs from the URL. Also, it should not be a rewrite but a REDIRECT with a http status return code as 301.

  4. Chris Turner says

    29 Sep 12 at 5:48 pm

    Thanks for the post Rick! For some reason I couldn’t find any of this information till I found your post. I wonder why the typical redirect 301 couldn’t be used? I used your example to move a site from using cold fusion to WordPress. Unfortunately, I can’t figure out why the home page redirects I found aren’t working. Example:

    RewriteCond %{HTTPS} (on)?
    RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
    RewriteCond %{REQUEST_URI} (.+)
    RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]

    Normal WordPress .htaccess uses:
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    I will say that I had to leave this in otherwise the ISAPI didn’t work right and 404 on me.

  5. IT Systems Services says

    15 Oct 12 at 8:40 pm

    The 301 is so important for SEO. Every time I move content I tell google through their webmaster panel that I just did a permanent 301 redirect and they seem to update my rankings pretty quickly.

  6. Martin says

    18 Oct 12 at 11:15 pm

    Hi Rick,

    thanks for your instruction. It seems that’s exactly that what I#m looking for to solve a problem.

    Maybe I did something wrong. I tried to redirect the URL from an page located in path a to a new page in path b. I did not use *.asp(x), my page is running with SSI.

    old: http://www.mydomain.de/path_a/page1.shtm
    new: http://www.mydomain.de/path_b/page2.shtm

    Using your syntax, I wrote in my .htaccess file (Windows Server 2008, IIS 7)
    RewriteRule ^/path_a/page1\.shtm /path_b/page2/ [R=301,L]

    But it didn’t work.
    I’m not very familiar with redirect rules, so I’m sure I’ve made something wrog.

    Please, do you know what to do.
    The page is running an a shared hosting service, so I do not have any access to the server configuration. Theres “only” a .htaccess file and a file called web.config

    Kind regards
    Martin

  7. Jason Holden says

    24 Oct 12 at 10:31 am

    Perfect! Just what I needed.

© 2012 Rick Tuttle, Papasoft