Forum Moderators: phranque

Message Too Old, No Replies

htaccess looping?

         

nrice

2:01 pm on Aug 28, 2007 (gmt 0)

10+ Year Member



Hi guys,

I need some help with the htaccess file setup. I have the following directives which basically map "www.domain.com" to "domain.com", and secondly does a bit of URL rewriting for the article pages. It works fine, until I access an article web page as "www.domain.com/article/some-title" (i.e. with the www). In this case the site becomes inaccesible, which I suspect is due to htaccess looping.

--------------------------------------------------------------------
IndexIgnore *

AddHandler application/x-httpd-php5 .php .php4 .php3 .phtml

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

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

RewriteRule ^article/([0-9]*)/(.*).html$ /article.php?id=$1
--------------------------------------------------------------------

An example article URL is like: http://example.com/article/9382/this-is-a-test-title.html (where 9382 is the article ID).

Please suggest how to fix the htaccess so that it doesn't loop and does what is required.

[edited by: jdMorgan at 2:20 pm (utc) on Aug. 28, 2007]
[edit reason] Example.com [/edit]

jdMorgan

2:19 pm on Aug 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's nothing wrong with the code you've posted, except for a three minor "holes" in the domain redirection and several inefficiencies that could be improved:

RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com/$1 [L,R=301]
#
RewriteRule ^article/([0-9]+)/(.+)\.html$ /article.php?id=$1 [L]

But none of these problems would cause a loop.

The most likely cause is that the hostname matched by the RewriteCond pattern does not exactly match the domain name in the RewriteRule substitution URL (shown here as "example.com").

A loop is easy enough to confirm; Use the "Live HTTP headers" extension for Firefox/Mozilla browsers, and watch the browser requests and server responses when pages at "www.example.com" are requested. By checking to see when a redirect is and is not invoked, you can determine whether the redirect code is correct. By looking at the "Location" header returned by the server in a redirect response, you can confirm that the substitution URL is correct.

Another reason to use a server headers checker is to be sure that your host isn't doing additional redirection or URL-rewriting, or that you don't have some setting in your "control panel" that is doing so. Additional rules added by the host or created by cPanel may well interact with your own, leading to a loop.

Jim

nrice

7:02 pm on Aug 28, 2007 (gmt 0)

10+ Year Member



Thanks a lot Jim! The fixes to those "holes" actually fixed the looping thing as well.