homepage Welcome to WebmasterWorld Guest from 54.226.235.222
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
non-www to www redirecting infinitely
lobo235




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

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




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

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




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

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.

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