Forum Moderators: phranque

Message Too Old, No Replies

Problem with non-www to www redirect :(

         

nickCR

10:58 pm on Jul 30, 2012 (gmt 0)

10+ Year Member



RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]


So this is the code that I am currently using.

I have it defined in example.com/beta/ directory and do not have a .htaccess in the example.com yet. However when I add the above directive, it redirects to www.example.com but does not maintain the /beta/.

Any idea whats going on here? Thanks in advance for your help.

phranque

11:21 pm on Jul 30, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



make sure to read carefully the documentation on Per-directory Rewrites:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

also, in most cases it is better to use a "if not canonical hostname" test:

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]

nickCR

11:33 pm on Jul 30, 2012 (gmt 0)

10+ Year Member



Code now looks like this:


Options -Indexes
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]


However this still does not work, it's just redirecting me to the root of the site and not staying in the beta.

g1smd

11:37 pm on Jul 30, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Redirects work in "per directory" context.

The higher folders are stripped before the RegEx is pattern matched and you aren't adding the stripped folders back on in the rule target.

The code is working exactly right for how it has been programmed.

Replace
!^www\.example\.com$ [NC]
with
!^(www\.example\.com)?$
too.

nickCR

11:56 pm on Jul 30, 2012 (gmt 0)

10+ Year Member



Thank you for your responses!

Ok code is now:


RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]


I thought ^(.*)$ captures the current directory and adds it at the end.

What might I look at for this situation?

g1smd

12:26 am on Jul 31, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



(.*) captures the current path after it has been "localised".

Usually this code goes in the root folder htaccess file.

If the code is in a folder, the folder details will need to be restated in the rule target.

nickCR

1:06 am on Jul 31, 2012 (gmt 0)

10+ Year Member



Ah ok that makes sense. Would "RewriteBase" help in this case
or that won't matter either?

Edit: Nevermind RewriteBase does not work.

However I added the beta to the rewrite so it looks like this:


RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule ^(.*)$ http://www.example.com/beta/$1 [R=301,L]


Now it's telling me that there is a redirect loop. Weird, very weird.

I also tried moving the above rule to the top-level directory. Obviously removing the /beta/ from there. That has resulted in a redirect loop on the top level for anything under the top-level domain.

Maybe I should make the redirect in PHP?

nickCR

2:05 am on Jul 31, 2012 (gmt 0)

10+ Year Member



Ok I'm an idiot. The problem is that I am using the HTML5 Boilerplate and it includes a redirect for www. to non www. They have the non www to www. commented. Uncommented and we're in business.

This is what they are using:


<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>


This is working with the sub-directories.

g1smd

6:26 am on Jul 31, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month






;;del

[edited by: g1smd at 6:27 am (utc) on Jul 31, 2012]

g1smd

6:27 am on Jul 31, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Dump their code and use the code supplied in this thread.

Their code contains the same flaws that the replacement code in this thread fixes.

Do add the
RewriteCond %{HTTPS} !=on

line to your code.

Remove the NC flag from the new rule too.

phranque

10:02 am on Jul 31, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



g1smd & i will respectfully disagree on this but i say keep the [NC] flag.

hostnames are case-insensitive.

Capital Letters (Pascal Casing) in URLs - Advantages and Disadvantages:
http://www.webmasterworld.com/google/3651999.htm [webmasterworld.com]

How Will Google See My Domain Name?:
http://www.webmasterworld.com/google/4450016.htm [webmasterworld.com]

RFC 1034 - Domain names - concepts and facilities
http://tools.ietf.org/html/rfc1034 [tools.ietf.org]:
By convention, domain names can be stored with arbitrary case, but domain name comparisons for all present domain functions are done in a case-insensitive manner, assuming an ASCII character set, and a high order zero bit. This means that you are free to create a node with label "A" or a node with label "a", but not both as brothers; you could refer to either using "a" or "A". When you receive a domain name or label, you should preserve its case.

RFC 1035 - Domain names - implementation and specification
http://tools.ietf.org/html/rfc1035 [tools.ietf.org]:
Note that while upper and lower case letters are allowed in domain names, no significance is attached to the case. That is, two names with the same spelling but different case are to be treated as if identical.

nickCR

5:18 pm on Jul 31, 2012 (gmt 0)

10+ Year Member



The one benefit to their code is that it works on the beta level without having to change anything.

Would you guys care to elaborate on why it's better to use one vs the other?

g1smd

7:29 pm on Jul 31, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Their code doesn't redirect all possible non-canonical hostname forms.

Rule order is important. The non-www/www code should be listed after all other redirects and before any rewrites.

nickCR

5:12 pm on Aug 1, 2012 (gmt 0)

10+ Year Member



g1smd thanks for the answer, makes sense.