Redirect when WWW not in URL

Topics: Developer Forum
Apr 13, 2011 at 4:00 PM
Edited Apr 13, 2011 at 4:00 PM

Hello.

I have a working redirect rule that I’m happy with, but recently I discovered that I’m actually missing a few visitors. If people type in our domain name, and DON’T use www at the start of the domain, my redirect rule doesn’t catch these people.

So I searched the discussion forum, and found some examples, and implemented the change. Here is what I now have in my INI file:

RewriteLog E:\Program Files\IIS Log Files\iirf

RewriteLogLevel 2

IterationLimit 10

MaxMatchCount 10

RewriteEngine ON

StatusInquiry ON

 

# Rule: stop using old domain name

RewriteCond %{HTTP_HOST}   ^(dev|www)\.olddomain.ca$ [I]

RedirectRule ^/(.*)$       http://www.newdomain.ca/$1         [R=301]

 

# Rule: Always use www prefix

RewriteCond %{SERVER_NAME} !^www\.newdomain\.ca$   [I]

RedirectRule ^/(.*)$   http://www.newdomain.ca/$1 [R=301]

 

The problem I’m having now is that it redirects when it’s just the domain name, but it doesn’t when it’s 2 folders deep. For example:

User types: newdomain.ca

Redirected: www.newdomain.ca (this works)

 

User types: newdomain.ca/About%20Us/Find%20Us.aspx

Redirected: redirect does not happen

 

Can you help me figure out what is wrong? Does it have something to do with HTTP_HOST versus SERVER_NAME ??

 

Coordinator
Apr 13, 2011 at 4:33 PM

It may have something to do with the different server variables you're using, but I doubt it. I do wonder why you'd use both.  Why not just use one of them?  Is there a reason you use HTTP_HOST for one rule, and SERVER_NAME for another?  You should make that consistent if possible, or document (in comments in the ini file) the reason for the difference.

Getting back to your question - I suspect it might be something to do with the URL decoding that is necessary on that URL.  Just looking at it here, it's not obvious to me what might be going wrong.

A clear indication can be had in the IIRF log file. Turn the log level to 4, run that URL through the IIRF, and send me the log file.

 

Apr 15, 2011 at 4:48 PM

Hi Cheeso.

I turned on logging to level 4, and have gone through some testing. Now that I have looked at it even closer (log level 4 can get pretty intense) I have come to some conclusions, and I think I need to change the rule(s).

We have 2 old domains, and I need to direct almost all requests for those old domains to the new domain name. I created the table below to help sort out what can and cannot be done.

User Types

Redirected To

www.olddomain1.ca

www.newdomain.ca

www.olddomain2.ca

www.newdomain.ca

dev.olddomain2.ca

www.newdomain.ca

olddomain1.ca

www.newdomain.ca

olddomain2.ca

www.newdomain.ca

dev1.olddomain2.ca

do not redirect

dev2.olddomain2.ca

do not redirect

 

The easiest way to describe it is to say that all old domains, with the exception if they use the virtual domains dev1 or dev2, need to be redirected to the new domain name.

Here is what I have been testing with right now, and I notice that it doesn’t handle the case where the user accesses the website with either of the dev1 or dev2 virtual domains.

 

RewriteLog E:\Program Files\IIS Log Files\IIRF\IIRF
RewriteLogLevel 4
IterationLimit 10
MaxMatchCount 10
RewriteEngine ON
StatusInquiry ON

# Rule: stop using old domain name

RewriteCond %{HTTP_HOST}   ^(dev|www)\.olddomain.ca$  [I]
RedirectRule ^/(.*)$       http://www.newdomain.ca/$1         [R=301]

# Rule: Always use www prefix 

RewriteCond %{HTTP_HOST} !^www\.newdomain\.ca$    [I]
RedirectRule ^/(.*)$    http://www.newdomain.ca/$1  [R=301]

 

I like the second rule, but its scope is too wide. Is there a way to make a rule that says “if you haven’t typed in www, then take the domain name you typed in and add www to the start”? If so, I could use that rule with my existing first rule – which does work.

Coordinator
Apr 15, 2011 at 9:51 PM

>  Is there a way to make a rule that says “if you haven’t typed in www, then take the domain name you typed in and add www to the start”?

Yes... Well, sort-of. There is a way to make a CONDITION that is true in that case. A condition is not a rule - it gets attached to a rule.  Attach that condition to your rule, and ba-da-boom, you're done.

Here is the condition:  

RewriteCond %{HTTP_HOST} !^www [I]
##(insert rule here)

The pattern in the condition evaluates to TRUE if the host does not begin with "www".   I think that is what you asked for.

I don't know what you're going to do with that condition and how you will combine it with the other condition you already have (dev|www). But maybe you can figure that out, on your own.  

Apr 21, 2011 at 9:25 AM
Edited Apr 21, 2011 at 12:38 PM

It works after a iisrestart.

Apr 25, 2011 at 6:02 PM

Going with that idea,... is there a way to say "if the URL is NOT www.newdomain.ca or if URL is NOT dev.olddomain.ca" ??

I know that the pipe is used as an OR statement, but can you use it across all of that, and still have the NOT statement?

Apr 27, 2011 at 5:42 PM

I managed to redirect one of our domains by using IIS's built in redirect feature: http://www.hpseo.co.uk/301-redirect-tutorial-iis/

Coordinator
Apr 28, 2011 at 4:07 PM

great!