Simple Non-WWW to WWW Question

Apr 30, 2010 at 6: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. http://domain.com/page.aspx to http://www.domain.com/page.aspx while still keeping:

http://www.domain.com/default.aspx redirecting to http://www.domain.com and

http://domain.com redirecting to http://www.domain.com.

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. 

RewriteCond %{REQUEST_METHOD} GET [I]
RedirectRule ^/default\.aspx(.*) /$1 [I,R=301]
RedirectRule ^/default\.htm(.*) /$1 [I,R=301]

Thanks in advance for your help!

- Matt

 

Coordinator
Apr 30, 2010 at 6:37 PM
Edited Apr 30, 2010 at 6: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:

RewriteCond %{REQUEST_METHOD} GET [I]
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 ... http://example.com/default.htm999, 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

RewriteCond %{REQUEST_METHOD} GET [I]
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:

RewriteCond %{REQUEST_METHOD} GET [I]
RedirectRule ^/(default|index)\.(aspx|htm)$  /   [I,R=301]

Good luck!

Apr 30, 2010 at 6:56 PM

Cheeso,

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 http://www.example.com/default.htm99 or http://www.example.com/blahblahthis-page-doesnotexist.html to 301 redirect to the root page, how would I modify the example?

Thanks again,

Matt

 

 

Coordinator
Apr 30, 2010 at 7: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 8:21 PM
Edited Apr 30, 2010 at 8:40 PM

Cheeso,

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.....

EXAMPLE:

# With a request like http://olddomain.com/Alpha.htm or http://olddomain.com/Alpha.asp (or
# if the olddomain is prefixed with www) , redirect (HTTP 301)
# the request to http://www.domain.com/Alpha.aspx

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

TRANSLATES TO:

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

RewriteCond %{REQUEST_METHOD} GET [I]
RedirectRule ^/(default|index)\.(aspx|htm)$  /   [I,R=301]

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

 

Coordinator
Apr 30, 2010 at 8: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 ^/(.*)$       http://www.example.com/$1         [R=301]
Apr 30, 2010 at 8: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? http://cheeso.members.winisp.net/Iirf20Help/html/b7a83a1c-5ae4-476b-83ef-34d2dab3c788.htm 

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

Coordinator
Apr 30, 2010 at 9:02 PM

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

 

Apr 30, 2010 at 9: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?

http://www.theinquirer.net/inquirer/news/1558680/berners-lee-regrets-double-slash

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

Coordinator
May 1, 2010 at 5:20 PM

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

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