Simple Non-WWW to WWW Question

Apr 30, 2010 at 5:10 PM

I'm new to the IIRF rewrite system with IIS6. I currently have the default.aspx page to redirect to the root, but the second rule I'm trying to add is redirecting all non-www traffic to the correct www location. It seems pretty simple, but I can't get it right. I don't want the page redirecting twice, if that makes sense.

i.e. to while still keeping: redirecting to and redirecting to

My current IIRF file is as follows:

RewriteFilterPriority HIGH
RewriteLogLevel 0

# Any incoming URL that starts with /default.aspx will be redirected
# to the root directory while preserving any query strings. 

RedirectRule ^/default\.aspx(.*) /$1 [I,R=301]
RedirectRule ^/default\.htm(.*) /$1 [I,R=301]

Thanks in advance for your help!

- Matt


Apr 30, 2010 at 5:37 PM
Edited Apr 30, 2010 at 5:40 PM

You don't have anything in there for www redirection.
Check the documentation, there's an example that does this.

Also, you have a RewriteCond on the first redirect, but not the second.  Why?

more likely you want:

RedirectRule ^/default\.(?:aspx|htm)(.*) /$1 [I,R=301]

Although, I have to say, I don't know why you'd redirect only on GET. Maybe you have your reasons.

Also, why put the thing that follows aspx or htm in the replacement URL? You do that with the $1.  The way you have your rule, if someone enters ..., then you'd redirect that request to /999 , which I think will probably not work. I mean to say, is there EVER a suffix to the default.aspx or default.htm URL that you want to append to / when redirecting? Likely not.

Which suggests that you really want

RedirectRule ^/default\.(aspx|htm)$  /   [I,R=301]

And broken URLs like /default.aspx999 will just get a 404, as they should, with no redirection.

Just for illustration purposes, you cuold take that just one step further, and also redirect on index.htm and index.aspx, like so:

RedirectRule ^/(default|index)\.(aspx|htm)$  /   [I,R=301]

Good luck!

Apr 30, 2010 at 5:56 PM


Thanks for your quick response! I ommitted the code for the non-www to www because I couldn't get it right. I was hoping there was an easy way to combine the two requests in a nice clean format. There is no reason for using the "GET", I just followed an example I found online. I had previously looked through the examples above, but I didn't know how to extract what I need.

I appreciate you showing how to condense the two lines into one, but how do I incorporate the non-www to www redirect? Also, just out of curiousity, let's say I DID want a redirect from a incorrect link like or to 301 redirect to the root page, how would I modify the example?

Thanks again,




Apr 30, 2010 at 6:51 PM

Matt, the examples are there.  There is no extraction required.  There are examples that do exactly what you asked for.


Apr 30, 2010 at 7:21 PM
Edited Apr 30, 2010 at 7:40 PM


I thank you for pointing me to the links, but as I mentioned I'm not sure how to put it together. If I understood the examples then I wouldn't be asking the question again. There is no example named "Non-WWW to WWW". I'm sorry for such "newbie" questions but I just don't understand how to piece the two together. I've used the "Domain Name Migration" example to attempt to merge both together.....


# With a request like or (or
# if the olddomain is prefixed with www) , redirect (HTTP 301)
# the request to

RewriteCond  %{HTTP_HOST}            ^(www\.)?olddomain\.com$
RedirectRule ^/(.+)\.(htm|asp)$       http:/$1.aspx         [R=301]


RewriteCond  %{HTTP_HOST}            ^(www\.)?\$
RedirectRule ^/(.+)\.(aspx)$       http:/$1.aspx         [R=301]

RedirectRule ^/(default|index)\.(aspx|htm)$  /   [I,R=301]

???? It doesn't work as expected.


Apr 30, 2010 at 7:49 PM

You appear to be missing a slash in the target of the redirection.  the text I see is http:/server, not http://server  .
The second slash is important.

Also in the text you posted you have a \m  in your RewriteCond, and no \ before the dot (.). 

That looks wrong to me. I think you want something like this:

RewriteCond  %{HTTP_HOST}  ^(?!www\.).*example\.com$
RedirectRule ^/(.*)$$1         [R=301]
Apr 30, 2010 at 7:56 PM

Thanks cheeseo, that's exactly what I was looking for! I took the example right from the links which is missing the slash, so maybe you can fix the slash there? 

I accidently typed in front of after the slash instead of before it when deleted the sample text. I'm not a network guru and don't have experience with HTACCESS either, so I appreciate you taking the time to help me! Have a nice weekend

Apr 30, 2010 at 8:02 PM

oo, good catch. thanks for the tip.  glad its working.


Apr 30, 2010 at 8:49 PM

Speaking of the missing "/" (and on a lighter note), did you see where Tim Berners-Lee (the inventor of the Internet), came out and said that requiring the "//" was completely unnecessary?

Great to know that we've been tracking all those slashes for nothing!

May 1, 2010 at 4:20 PM

Ha, no I hadn't seen that.  NOW he tells us.

By the way I fixed the missing slash in the documentation.