Forum Moderators: phranque

Message Too Old, No Replies

Assistance needed to modify existing .htaccess

         

dirtymonkey

10:05 am on Jul 3, 2009 (gmt 0)

10+ Year Member



Hi all,

The following example routes all mydomain.com traffic to www.mydomain.com


########## Begin - Rewrite rules to enhance SEO SEF
#
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com\. [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com:[0-9] [NC]
RewriteCond %{SERVER_PORT}>s ^(443>(s)¦80>s)$
RewriteRule (.*) http%2://www.mydomain.com/$1 [R=301,L]
#
########## End - Rewrite rules to enhance SEO SEF

How do I do the reverse and route all www.mydomain.com traffic to mydomain.com?

Also how would I modify either to incorporate a sub domain?

Thanks in advance, DM.

g1smd

10:23 am on Jul 3, 2009 (gmt 0)

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



Add
www\.
to conditions where it is missing.
Remove
www\.
from those that already have it.
Remove
www.
from the target URL.

You'll need to explain what you mean by "incorporate a subdomain". You already have one: www.

dirtymonkey

10:55 am on Jul 3, 2009 (gmt 0)

10+ Year Member



Thanks g1smd, so to add (another) sub domain:

########## Begin - Rewrite rules to enhance SEO SEF
#
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.subdomain\.mydomain\.com\. [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.subdomain\.mydomain\.com:[0-9] [NC]
RewriteCond %{SERVER_PORT}>s ^(443>(s)¦80>s)$
RewriteRule (.*) http%2://www.subdomain.mydomain.com/$1 [R=301,L]
#
########## End - Rewrite rules to enhance SEO SEF

jdMorgan

2:35 pm on Jul 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This 'reverses' the logic in your original rule, so that "www" is redirected to non-www:

########## Begin - Rewrite rules to enhance SEO SEF
#
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.com\. [NC,OR]
RewriteCond %{HTTP_HOST} ^example\.com:[0-9] [NC]
RewriteCond %{SERVER_PORT}>s ^(443>(s)¦80>s)$
RewriteRule ^(.*)$ http%2://example.com/$1 [R=301,L]
#
########## End - Rewrite rules to enhance SEO SEF

However, as g1smd says above, it would be helpful if you would tell us what you want to do when your "added" non-www subdomain is requested. Do you want to redirect it to add www? Do you want to redirect it to the canonical "example.com"? What is your goal or requirement here?

Jim

dirtymonkey

3:54 pm on Jul 3, 2009 (gmt 0)

10+ Year Member



Thanks Jim, so to add a sub-domain to your example would be as follows?


########## Begin - Rewrite rules to enhance SEO SEF
#
RewriteCond %{HTTP_HOST} ^www\.subdomain\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com\. [NC,OR]
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com:[0-9] [NC]
RewriteCond %{SERVER_PORT}>s ^(443>(s)¦80>s)$
RewriteRule ^(.*)$ http%2://subdomain.example.com/$1 [R=301,L]
#
########## End - Rewrite rules to enhance SEO SEF

For SEO purposes I know its generally better to use one or the other but would you recommend to redirect to www.example.com or the canonical "example.com"

jdMorgan

4:23 pm on Jul 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Again, when your "added subdomain" is requested, what do you want to happen?

We cannot answer unless you ask the question very clearly.

Jim

g1smd

7:01 pm on Jul 3, 2009 (gmt 0)

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



I always use the www version of each URL. I like the fact that
site:www.example.com
lists all the pages of the site, and
site:example.com -inurl:www
should show nothing, or will show any Duplicate URLs or other 'escaped' sub-domains.

dirtymonkey

7:25 pm on Jul 3, 2009 (gmt 0)

10+ Year Member



That's great guys thanks for your invaluable assistance, DM.