Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite subdomains and main domain

Need to canonicalize both

         

aimless

5:29 am on Dec 28, 2009 (gmt 0)

10+ Year Member



I have say domain called example.com and some 30-40 odd subdomains like
abc.example.com, bcd.exaple.com, ... xyz.example.com

I want that my main domain is browsed with www i.e.
www.example.com
I have already made entry for this

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

and all my sub-domains are browsed without www i.e.
[abc.example.com...]
[bcd.example.com...]
.......
[xyz.example.com...]

Can anyone help.

jdMorgan

5:28 am on Dec 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# Redirect to canonicalize example.com, www.<subdomain>.example.com, <subdomain>.example.com.,
# <subdomain>.example.com:80, and <subdomain>.example.com.:80, etc.
RewriteCond www.%{HTTP_HOST} ^(www)\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.([a-z0-9\-]+)\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z0-9\-])\.example\.com(\.¦\.?:[0-9]+)$ [NC]
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]

Important: Replace the broken pipe "¦" character with a solid pipe character before use; Posting on this forum modifies the pipe characters.

As written, this code is for use in the top-level .htaccess file or within a <Directory> container in a server config file such as httpd.conf. If this code is to be used in a server config file outside of any <Directory> container, then change the RewriteRule pattern to "^/(.*)$" -- i.e. add the leading slash before the parentheses.

Jim

aimless

8:04 am on Dec 29, 2009 (gmt 0)

10+ Year Member



Thanx morgan. I have implemented it and works completely fine. I could understand except [NC,OR] and [R=301,L] (Although I understand that 301 is there to do permanent redirection). Rest everything is fine. I could not find any good tutorial to understand the syntax above which I am talking about.

g1smd

9:35 am on Dec 29, 2009 (gmt 0)

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



NC = aNy Case.

OR = Logical this line OR the next one is true.

L = Last rule to process on this pass.

khuram

10:49 am on Dec 29, 2009 (gmt 0)

10+ Year Member



Hi Jim,

I have a similar problem. however, any subdomains I put in the address bar give a straight server not found without going to the main domain.
What should be the remedy? should I ask hosting to change something and what ?

khuram

11:34 am on Dec 29, 2009 (gmt 0)

10+ Year Member



Hi,

Ok after talking to our hosting. I have added * as a subdomain.
I need it to propogate first though.
Also, is there a security threat in this?

jdMorgan

9:26 pm on Dec 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Security threat? What do you have in mind here? You are simply defining any/all possible subdomains to point to your server by using the wild-card. The alternative is to explicitly define all 'valid' subdomains one-at-a-time -- and I don't imagine you'd enjoy trying to keep up with that over a span of years...

Jim