homepage Welcome to WebmasterWorld Guest from 54.145.238.55
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
301 Canonical redirect - non-www to www but it messes with subdomains
madmatt69

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4345202 posted 9:26 pm on Jul 28, 2011 (gmt 0)

So I've got the following rewrite code in my htaccess:
RewriteCond %{HTTP_HOST} !^www.canuckabroad\.com [NC]
RewriteRule ^(.*) http://www.canuckabroad.com/$1 [QSA,R=301,L]


Works perfect for redirecting non-www to www for my domains. However it breaks my subdomains! I've got a subdomain, lets call it 'test.mysite.com' which works find if I remove that canonical code, otherwise it redirects to www.mysite.com.

Any ideas? Is there a way to 'whitelist' a subdomain so it doesn't get caught up in that redirection?

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4345202 posted 10:13 pm on Jul 28, 2011 (gmt 0)

However it breaks my subdomains! I've got a subdomain, lets call it 'test.mysite.com' which works find if I remove that canonical code, otherwise it redirects to www.mysite.com

Time for a chorus of "I really hate that ### machine" et cetera. OK, so the blasted .htaccess is doing exactly what you told it to do. How 'bout replacing the condition with

RewriteCond %{HTTP_HOST} ^canuckabroad\.com

so it bypasses all subdomains. Now, if you're concurrently throwing out some-but-not-all subdomains and want them, too, to end up at www.why-is-it-so-warm.com, then you have to start adding stuff.

madmatt69

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4345202 posted 10:23 pm on Jul 28, 2011 (gmt 0)

Thanks!

Just gave that a shot, no dice - still redirecting the subdomain.

That's the frustrating part - I want to to redirect all the other stuff but leave that subdomain alone.

Is there a way to whitelist a subdomain, so to speak?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4345202 posted 10:50 pm on Jul 28, 2011 (gmt 0)

Did you clear the browser cache each time? Redirect status is cacheable by the browser.

You want example.com => www.example.com here, but do you want to add or strip www from the other subdomains? You should do something for those too.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4345202 posted 3:00 am on Jul 29, 2011 (gmt 0)

You mean that all subdomains except one specific one are all supposed to end up at www.it-isn't-called-ice-hockey.com? Then you need one more line in your conditions:

RewriteCond %{HTTP_HOST} !^www\.canuckabroad\.com [NC]
RewriteCond %{HTTP_HOST} !nicesubdomain\.canuckabroad\.com

That is: if the host isn't www.so-you-call-that-english-eh.com, and also isn't the subdomain you want to protect, then proceed to the Rule. In Conditions, the default connector is AND: the whole list has to be true. Just pile on the conditions. You can also merge them if there's more than one Good Subdomain:

RewriteCond %{HTTP_HOST} !(nicesubdomain|evennicersubdomain)\.canuckabroad\.com

hottrout



 
Msg#: 4345202 posted 4:15 pm on Aug 1, 2011 (gmt 0)

This is an interesting post as I am having similar issues. I have two subdomains for my site. One is for the forums forum.example.com and one is for cookieless site content like images, css etc, sitefiles.example.com. I also have the following code to redirect all non www to the www.example.com domain.

I have noticed (using Xenu) that because of the non www to www code there are thousands of 301 redirects happening for the forums and the sitefiles, and I suspect that this can not be good for page speed.

Should i also change the code to :-

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

If I make the changes that you have suggested above, what are the implications? Better/Worse speed? Better or worst SEO? I want to understand what best practive would look like regarding this and what the experts would reccomend.

Also how do I ensure that the subdomain for sitefiles is cookieless?

Thank you in advance.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4345202 posted 4:39 pm on Aug 1, 2011 (gmt 0)

It's generally better (i.e. faster, simpler and safer) to whitelist. If you've got forum.example.com, sitefiles.example.com, and the occasional visitor typing in plain example.com without leading www, then just redirect the ones whose original request is exactly equal to

^example.com

rather than putting the computer through the extra loop of "not www.example.com BUT ALSO NOT sitefiles.example.com AND ALSO NOT forum.example.com".

And, of course, make sure all your internal links are in the desired form in the first place.

hottrout



 
Msg#: 4345202 posted 5:03 pm on Aug 1, 2011 (gmt 0)

OK, so why is the original form

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

and not just

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

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4345202 posted 11:00 pm on Aug 1, 2011 (gmt 0)

Everything is different when subdomains get involved. Ordinarily it's just an either-or:

example.com OR www.example.com

But now you've got a whole list-- including, as g1 points out above, a further nest of www-vs.-non-www issues:

example.com
www.example.com
subdomain.example.com
www.subdomain.example.com

(I tend to forget this too. The subdomain isn't instead of www, it's in addition-- but most people will want to end up with no-more-than-one-word.example.com)

There are a couple different ways of wording the conditions. If you want all subdomains to be treated the same, I think everything will be covered by

RewriteCond %{HTTP_HOST} !^(www|nicesubdomain)\.example\.com [NC]
RewriteRule (.+) http://www.$1

--that is, if the input doesn't start with www or one of your subdomains.

hottrout



 
Msg#: 4345202 posted 11:16 am on Aug 2, 2011 (gmt 0)

Lucy,

I used the following code but it caused a 500 server error

RewriteCond %{HTTP_HOST} !^(www|sitefiles|forum)\.example\.com [NC]
RewriteRule (.+) [$1...]

Am I missing something. I want as you explained to not redirect sitefiles.example.com or forum.example.com but to redirect all other non www traffic to www.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4345202 posted 12:05 pm on Aug 2, 2011 (gmt 0)

Looks like a request for
http://foo.example.com/thatpath will be 302 redirected to http://www.thatpath.

Additionally, the ending
)?$ in the original pattern is necessary for a "not exactly" match instead of the lesser "begins with" match.
hottrout



 
Msg#: 4345202 posted 12:36 pm on Aug 2, 2011 (gmt 0)

Does this then work better

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

I have a feeling I am going to get shouted at now >)

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4345202 posted 2:12 pm on Aug 2, 2011 (gmt 0)

That looks a bit bettter, but $2 will be undefined (empty) all the time, so will redirect to root for all requests.

Lose the [NC] flag. It blurs the wanted "exactly" match, by allowing "exactly" and "similar but with different case". You need "exactly".

hottrout



 
Msg#: 4345202 posted 3:02 pm on Aug 2, 2011 (gmt 0)

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

Is this correct? How does the $1 work in this configuration? or if does not then how do I pass the start of the url on for the redirect?

Maybe you could write the code accuratly and explain your solution. I am feeling stoopid again. Thanks.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4345202 posted 3:47 pm on Aug 2, 2011 (gmt 0)

That should work.

$n comes from the RegEx pattern in the RewriteRule.

%n comes from the RegEx pattern of the last matching RewriteCond.

In both cases, the number n can be worked out by counting the left parentheses.

hottrout



 
Msg#: 4345202 posted 3:58 pm on Aug 2, 2011 (gmt 0)

Thats where i was going wrong. The $1 referes to the RewriteRule. Can I also ask if you had any luck finding out why google was creating so many malformed URLs in the webmaster crawlerror tool?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4345202 posted 4:03 pm on Aug 2, 2011 (gmt 0)

I haven't tracked that down. I also have some other bot requesting thousands of those malformed URLs. I can't decide if the bot has misread the HTML source code or there's a fundamental problem in the .htaccess file somewhere. No other UAs exhibit any problem, and there's nothing obvious when manually looking at the HTML source code for any page.

hottrout



 
Msg#: 4345202 posted 4:19 pm on Aug 2, 2011 (gmt 0)

The number of malformed urls reported by my webmaster tools increases every few days and I can not figure out why. It is a complete mystery to me. Please do PM me if you find a cause or possible problem. I am more content now (after your help) that my htaccess is not the root cause of the problem.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4345202 posted 6:59 pm on Aug 2, 2011 (gmt 0)

I used the following code but it caused a 500 server error

I can well believe it. Where's the "crawling under the carpet in shame" emoticon when you need it? I can't remember what I actually meant to write, but I sure hope it wasn't what I did write.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4345202 posted 11:47 pm on Aug 2, 2011 (gmt 0)

Error 500?

Great! Only 499 to go.

credits: jdMorgan

hottrout



 
Msg#: 4345202 posted 3:43 pm on Aug 3, 2011 (gmt 0)

On a related note but somewhat off topic none the less,

I decided not to include forum in the list of subdomains to be ignored because I thought rightly or wrongly that this would have implications for my already google indexed pages which link back to www.example.com/forum/

Am I correct in thinking this? If google has already indexed the forum as www.example.com/forum/ and I allow the subdomain to be used changing it to forum.example.com will result in PR being split across what google will see as two different sites.

Is it better to use forum.example.com and then redirect everything from www.example.com/forum to forum.example.com with a 301?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4345202 posted 6:38 pm on Aug 3, 2011 (gmt 0)

Whatever you do, the links to other pages of the forum from within the forum should point to the correct URL. Clicking internal forum links should not take the user through a redirect.

You should redirect the other URL variant to prevent those becoming a duplicate.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved