Forum Moderators: phranque

Message Too Old, No Replies

Problems with RewriteRule directive

         

JSayre

9:58 pm on Mar 14, 2007 (gmt 0)

10+ Year Member



I am a brand-new member--joined today! I have experience (15 years ago) programming, but have only recently returned. I'm basically a newbie when it comes to PHP, PERL, etc. I'm comfortable with xhtml coding.

I'm trying to learn more about Apache services. Right now, I'm slowly working my way through understanding the mod_rewrite module and have basically failed in my attempts at using mod_rewrite directives within .htaccess files.

I've hit two snags. Here they are:

1. I'm trying to reroute all requests for "domain.html" to "www.domain.html". Obviously, I'm using my real domain names in my code, but to abide by the posting rules, I'm using these generic ones for example. Here's the code I'm using.

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

It seems simple enough, but when I load my browser and type "example.com", it successfully rewrites and the browser reloads to read "http://www.example.com/" but then after a few seconds, my browser throws this error message:

Too many redirects occurred trying to open "http://www.example.com/". This might occur if you open a page that is redirected to open another page which then is redirected to open the original page.

I'm browsing with Safari v2.0.4. But, other browsers also report issues.

2. I'm trying to redirect all requests for one of my podcast feed files from the original xml file to a url that is then (via a CNAME record in the domain's DNS table) redirected to my burned feed at FeedBurner. The key here is to redirect all requests except those from the FeedBurner bot since it must access the original feed file to find updates. That is why I cannot use a simple Redirect directive.

I do not receive any errors but it does not result in requests being rerouted to the new feed location. In effect, it does nothing.

Once again, I've changed the actual names and made them generic. Here's the code I'm using.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT}!FeedBurner
RewriteRule ^/podcasts/abc/xyz\.xml$ [podcasts.example.com...] [R,L]

This code is in a directory-level .htaccess file. The directory is entitled "podcasts". Since it is in the "/podcasts" directory, I did remove that reference from the path in the ReWrite rule but it did not make a difference.

I have a dedicated server through DreamHost (I hope that's okay to state). Although the server is all mine to use, I do not have access to the httpd.conf file.

Here is the relevant server data:

* Apache/1.3.37
* mod_throttle/3.1.2
* mod_fastcgi/2.4.2 (although I do not have this enabled for the domain in question)
* mod_gzip/1.3.26.1a
* PHP/4.4.4

I can request an update to Apache 2.2 and I can choose to use PHP 5.2.1 instead (which I am planning to do).

I've conferred with DreamHost and FeedBurner, and whereas they have both been very helpful, the problem is not solved.

I've looked at the error log file but (assuming that it is updated in real time), I do not see any message that would indicate an issue. In fact, when I upload a version of the .htaccess file that I know causes an error, when I look at the error log, the most recent entry is many hours old (accounting for the time difference between me and my hosting firm).

Any insight into these two issues would be much appreciated

Thanks,
Jeff

[edited by: jdMorgan at 10:33 pm (utc) on Mar. 14, 2007]
[edit reason] Example.com [/edit]

jdMorgan

10:27 pm on Mar 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1. The following code uses "!" to indicate NOT. So therefore, if all you changed was the "example.com" part of both lines, I don't see how you could be stuck in a redirection loop. I'd suggest using the Live HTTP Headers extension with Firefox to investigate the exact request/response sequence you're getting.

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

The first RewriteCond is needed if it is possible to make an HTTP/1.0 request from your server. HTTP/0.9 and HTTP/1.0 did not support the Host: header, which would leave the HTTP_HOST variable empty, and would put HTTP/1.0 clients into a redirection loop if this blank-check is not included.

An alternative, if your server has some funky or broken regular-expressions library, would be to detect the specific non-www variant, and redirect only that.


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

Oh, one more thing... If your ServerName is NOT www.domain.com, then make sure that UseCanonicalName is Off. It's possible this may be causing all or part of this problem.

2. In httpd.conf or conf.d (server config context) RewriteRule patterns must allow for the full local URL-path, including the leading slash. However in .htaccess, the path to the current directory is removed. Therefore, the leading slash will not be present, so your second rule will never match in .htaccess.

Jim

[edited by: jdMorgan at 10:31 pm (utc) on Mar. 14, 2007]

JSayre

3:34 pm on Mar 15, 2007 (gmt 0)

10+ Year Member



Jim:

Thanks for the response. I really like the Live HTTP Headers extension for Firefox!

This is the outcome of the test:

The Generator List:

#request# GET http://example.com/
GET /
#request# GET http://www.example.com/
#redirect# GET /
#request# GET http://www.example.com/
#redirect# GET /

The last two lines are repeated 18 more times

Basically, it redirects the first time successfully then it keeps hitting a 301 with the new location repeated in a loop. I thought the [L] flag would stop the process after the first successful RewriteRule?

HTTP Headers report:

http://example.com/

GET / HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Thu, 15 Mar 2007 15:05:19 GMT
Server: Apache/1.3.37 (Unix) mod_throttle/3.1.2 DAV/1.0.3 mod_fastcgi/2.4.2 mod_gzip/1.3.26.1a PHP/4.4.4 mod_ssl/2.8.22 OpenSSL/0.9.7e
Location: [www....] example.com/
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
[www....] example.com/

GET / HTTP/1.1
Host: www. example.com
User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.x 301 Moved Permanently
Date: Thu, 15 Mar 2007 15:05:20 GMT
Server: Apache/1.3.37 (Unix) mod_throttle/3.1.2 DAV/1.0.3 mod_fastcgi/2.4.2 mod_gzip/1.3.26.1a PHP/4.4.4 mod_ssl/2.8.22 OpenSSL/0.9.7e
Location: [www....] example.com/
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1

Again, the last grouping is repeated 18 more times

I did try this suggestion of yours several days ago:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ example\.com [NC]
RewriteRule (.*) [www....] example.com/$1 [R=301,L]

Nothing happens with this code. The rewrite rule is not fired.

So, it could (must?) be that UseCanonicalName is On when it needs to be off. The ServerName is not www.domain.com.

Unfortunately, even though this is a dedicated server, DH does not give access to the http.conf files. So, I cannot inspect the contents and make changes.

I guess my next step is to ask them to upgrade Apache to version 2.2 and then specifically request certain config settings like:

AllowOverride All
Options All
UseCanonicalName Off
RewriteEngine on (for all directories)

Thanks again for your help.

Jeff

jdMorgan

4:00 pm on Mar 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, based on your originally-posted negative-match code, and on the results of testing the positive-match code, I'd say that server has a problem -- A mis-configured or bad Apache install, or a bad POSIX library in the OS load.

When you specifically told it to redirect only if the Host header request is NOT www- and sent it a www- request, it redirected anyway. So something is seriously wrong or mis-configured, here.

Jim