Welcome to WebmasterWorld Guest from 107.21.175.43

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

non-www to www redirecting infinitely

     

lobo235

7:33 pm on Sep 22, 2007 (gmt 0)

10+ Year Member



Can anyone tell me why the following is redirecting infinitely? I have placed this in my httpd.conf in a Directory section:

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

[edited by: jdMorgan at 7:40 pm (utc) on Sep. 22, 2007]
[edit reason] example.com [/edit]

jdMorgan

7:50 pm on Sep 22, 2007 (gmt 0)

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



I don't see anything catastrophically wrong with that, but be aware that RewriteRule patterns in code for httpd.conf differ slightly from that used in .htaccess. Specifically, the URL-path matched by RewriteRule patterns in .htaccess will be stripped of the leading slash, whereas this leading slash will be present in the URL-path seen by RewriteRule in http.conf.

So, this code should be modified slightly in either of the following ways:


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

-or-

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

I removed the [NC] from the RewriteCond patterns as well; It will simplify many of your other rules if you enforce not only the canonical domain name "spelling," but specifically the all-lowercase canonical domain name.

If either version of the modified code does not help, and if www.example.com is not the primary ServerName (with example.com as the ServerAlias), then make sure your server is configured with UseCanonicalName off.

Jim

lobo235

2:10 am on Sep 23, 2007 (gmt 0)

10+ Year Member



Great, thanks for the help. After implementing the redirect in the way you mentioned I was still having trouble. It turns out that part of my PHP code was causing the infinite redirect.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month