mod_rewrite and slash problem

Tag: apache , mod-rewrite Author: bud2009 Date: 2011-04-04

How to redirect url`s like this

http://example/foo/bar/

http://example/foo/bar

to

http://example/foo/bar.php

Here is my rewrite rule

RewriteRule ^(.*)$ $1.php

It works with http://example/foo/bar but if the url is ending with slash a error rise (Internal Server Error) What to to do so both url types to work properly ?

Other Answer1

It's because the / got matched in the group .*, so you call http://example/foo/bar/.php

To make both works use :

RewriteCond %{REQUEST_URI} !\.php
RewriteRule ^(.*)/?$ $1.php

EDIT: forget when we ask a file in the first place

comments:

Thanks but it still give me Internal Server Error if the url is like example.com/foo/bar
Here is dump from error_log: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary
Yes, cause I matched the files as well. RewriteCond shall avoid that. All .php files call does not get rewritten.

Other Answer2

Well, if this is really what you want to do, then:

RewriteRule  ^(.*)/?$  $1.php

I haven't tested this, but I think it works.