homepage Welcome to WebmasterWorld Guest from
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

Rewrite Question.httpd.conf - no subdomains

10+ Year Member

Msg#: 3670631 posted 6:33 pm on Jun 9, 2008 (gmt 0)

Hi, I have succesfully implemented some rewrite code in my http.conf file, for redirecting pages to the www.domain.com site instead of the non domain.com site.

I am wondering if anyone could help me with this code to add something that says: do not redirect to the www.domain.com version of the page if the domain is a subdomain ie: test.domain.com

So, I'd like to retain my redirects for
domain.com goes to www.domain.com
but no redirects if it is subdomain.domain.com

Could anyone help me with this please? Thank you!

Here is my generic code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^\.]+\.[^\.]+$
RewriteRule ^/(.*)$ [%{HTTP_HOST}...] [R=301,L]



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

Msg#: 3670631 posted 10:24 pm on Jun 9, 2008 (gmt 0)

Your code, as shown above, will not redirect requests for subdomains. The RewriteCond pattern says, "Match one or more characters starting with or containing anything but a period, followed by a period, followed by and ending with one or more characters which are anything but a period. In other words, only one period is allowed, otherwise the RewriteCond will not match and the rule will not be applied.

You might want to look around in the other config files, as it's possible that another rule is responsible for the unexpected redirection of your subdomains.

There's also one latent defect in the rule above: It could be unexpected defeated by perfectly-valid, but non-canonical requests for FQDNs or domains with appended port numbers -- or both. Ane example would be: [google.com.:80...] which is perfectly-valid, but non-canonical.

This can be handled properly by using two RewriteConds -- one being a modified version of what you already have:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+\.[^\.]+)\.?(:[0-9]+)?$ [OR]
RewriteCond %{HTTP_HOST} ^www\.([^\.]+\.[^\.]+)(\.(:[0-9]+)?:[0-9]+)$
RewriteRule ^/(.*)$ http://www.%1/$1 [R=301,L]

Replace the broken pipe "¦" character in the second RewriteCond pattern with a solid pipe before use; Posting on this forum modifies the pipe characters.



10+ Year Member

Msg#: 3670631 posted 2:12 pm on Jun 10, 2008 (gmt 0)

Hi Jim,

Thank you very much for the help on the rules!


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