Forum Moderators: phranque

Message Too Old, No Replies

Another story of www and non-www problem

the problem that occurs to subdomain

         

Pat1975

5:58 am on Jul 18, 2005 (gmt 0)

10+ Year Member



I've research this site about the www and non-www issues, I got a lot of information but my problem seems to not be address here yet. So, here is my situation:

I have a www.domain.com and serveral subdomain.domain.com. Recently, in Google index, all of these sites are replaced with "domain.com" and "www.subdomain.domain.com".

So, I need advice on how to redirect the

www.subdomain.domain.com to subdomain.domain.com

and

domain.com to www.domain.com

I found the following code somewhere in this forum

RewriteEngine On
RewriteCond %{HTTP_HOST}!^www\.widget\.com
RewriteRule (.*) [widget.com...] [R=301,L]

but I'm afraid that it will redirect all the subdomain to the www.domain.com site.

Another question is if it possible to disable the www.subdomain.domain.com and the domain.com.

jdMorgan

4:29 pm on Jul 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Pat1975,

You'd undoubtedly be happier using a positive-match pattern, instead of the negative match used in that example. The code you posted redirects anything *except* www.widget.com to www.widget.com.


RewriteEngine On
#
# Redirect www.<subdomain>.example.com/<anything> to <subdomain>.example.com/<anything>
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule (.*) http://%1.example.com/$1 [R=301,L]
#
# Redirect example.com/<anything> to www.example.com/<anything>
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

This example should do what you need. However, I cannot recommend that you use any code posted in these forums unless you study it and understand it. The resources cited in our forum charter [webmasterworld.com] can be used to look up the meanings of each of these regular-expressions patterns and mod_rewrite directives. Understanding the code, testing, and examining your error logs will go a long way toward preventing potentially-serious configuration problems on your server.

Jim

Pat1975

4:50 am on Jul 19, 2005 (gmt 0)

10+ Year Member



Thank you very much Jim. I have another question, I think that there may be some people would try to visit http://example.com, but, there would be very few try to visit http://www.subdomain.example.com. So, I think I would better to disable it instead of redirect it. Is it possible to do it via the .htaccess file?

[edited by: jdMorgan at 5:18 am (utc) on July 19, 2005]
[edit reason] example.com, please. [/edit]

jdMorgan

5:18 am on Jul 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I think I would better to disable it.

Well, I don't agree, but you could do that. However, I'd recommend you simply reverse the two rulesets above for the sake of efficiency (process example.com requests first), and then forget about it. Those few who try to access your site by its alternate names will be redirected, find what they are looking for and be happy, and the performance impact on your server will be negligible. Similarly, search engine spiders, if they happen to find a link to one of your subdomains, will be redirected as well, and will therefore transfer the link credit to the correct domain and list it instead of the subdomain.

It's a more pleasant user experience that getting "slapped in the face" with an unexpected "403-Forbidden" or "Domain cannot be resolved" error message.

Jim

g1smd

11:00 am on Jul 25, 2005 (gmt 0)

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



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

g1smd

10:46 am on Jul 27, 2005 (gmt 0)

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



Also try:

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

The extra ^ and $ may be important.