Welcome to WebmasterWorld Guest from 23.21.38.201

Forum Moderators: Ocean10000 & incrediBILL & phranque

Rewrite Problem

non-www to www redirect is breaking subdomains

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

10+ Year Member



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]

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

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



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

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

10+ Year Member



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]

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

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



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

Jim

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month