views:

211

answers:

4

I'm using the following code in my .htaccess:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]

# Special rewrite rules
# ideas/<id>
RewriteRule ^ideas/([0-9]+)$ idea\?id=$1
# users/<name>
RewriteRule ^users/(.+)$ users\?name=$1

The ideas/ rule works fine, as I'd expect it to, but the users/ rule doesn't seem to. It gives me a HTTP 500 error and the Apache log says it's exceeded the amount of redirects available:

[Sun Jun 14 10:58:39 2009] [error] [client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://localhost/users

The url I'm testing it on is /users/ross, which should work fine. /ideas/1 definitely does work fine.

+1  A: 

Try it in this order:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]

RewriteRule ^ideas/([0-9]+)$ idea?id=$1
RewriteRule ^users/(.+)$ users?name=$1

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
Gumbo
This causes the ideas/<id> pages to stop working with the same error.
Ross
And what does the server log say about that? A 500 error can mean anything.
Gumbo
Updated the question.
Ross
Is MultiViews disabled? If not, try to disable it. See http://httpd.apache.org/docs/2.2/content-negotiation.html#multiviews
Gumbo
+1  A: 

What might help you to debug the situation is to make the rewrites remote (e.g. make them send HTTP redirects). This way you'll see how the requests are rewritten which should help you catch the problem.

che
+1  A: 

Am I right to assume you've got a users.php and idea.php file? Then you can redirect to that file directly, without the need for yet another rewrite round. So, for those two rules:

RewriteRule ^ideas/([0-9]+)$ idea.php?id=$1
RewriteRule ^users/(.+)$ users.php?name=$1

(also: no need to escape the question mark in the result)

By the way: Why is users.php plural while idea.php is not? How are you handling /ideas/abc and so on?

Arjan
The plural bit was a mistake, I've corrected that since. Changing the redirects to point to the files directly works however (although I'd be interested in finding out why it doesn't obey the earlier rules).
Ross
There is no sub folder "users" or "ideas" on your file system that could have caused the difference between the two results? I feel the earlier rules were just incomplete (and so is your own answer, in my opinion)... When specifying the script you can even add [L] to indicate you're done.
Arjan
A: 

Adding a / before the files fixed this:

RewriteRule ^ideas/([0-9]+)$ ideas?id=$1
RewriteRule ^users/(.+)$ users?name=$1

to:

RewriteRule ^ideas/([0-9]+)$ /ideas?id=$1
RewriteRule ^users/(.+)$ /users?name=$1
Ross
You're not using some path in the URL, like example.org/folder/users/abc, right? And why not rewrite to the script you need rather than having Apache figger out what to do with /users?name=$1?
Arjan