homepage Welcome to WebmasterWorld Guest from 54.196.77.82
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 Problem
non-www to www redirect is breaking subdomains
lobo235




msg:3220280
 10:11 pm on Jan 15, 2007 (gmt 0)

I implemented some code to redirect non-www requests to their www counterpart but this has broken my subdomains. For example, if I visit sub.example.com I am redirected to www.example.com/sub

I need to be able to redirect non-www traffic to www but also be able to use subdomains. Can anyone help me understand how this would be done? Also, if you see any other problems with the way I have implemented this please let me know.

Here is what I have in my .htaccess:

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

 

jdMorgan




msg:3220286
 10:17 pm on Jan 15, 2007 (gmt 0)

You can either exclude all your known subdomains by adding them to the RewriteCond, or you might elect to redirect only example.com to www.example.com, instead of redirecting anything except www.example.com to www.example.com -- which is what your current code is doing.

Jim

lobo235




msg:3220329
 10:50 pm on Jan 15, 2007 (gmt 0)

I have now updated it to the following and it is working great. Thanks for the excellent help as always jdMorgan.

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

jdMorgan




msg:3220373
 11:28 pm on Jan 15, 2007 (gmt 0)

You can remove the second RewriteCond, since it will always be true if the first RewriteCond is true.

Jim

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