topherg
topherg

Reputation: 4293

Trailing slashes causing error code 500

I have this htaccess script

Options -Indexes
Options +FollowSymlinks

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /$1.php [L,QSA]

So i can get access a page like example.com/test but when i try to go to example.com/test/ it throws a 500 error. Can someone tell me what needs to change for it to work? Probably a stupid error.

Upvotes: 2

Views: 1981

Answers (1)

LazyOne
LazyOne

Reputation: 165118

Change pattern from ^(.+)$ to one that will handle trailing slash separately: ^(.*[^/])/?$

Options -Indexes +FollowSymlinks

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])/?$ /$1.php [L,QSA]

This will work for both /test and /test/ -- both will be rewritten to /test.php.

If you want different behaviour, to have /test working but have "404 Not Found" error for /test/ (instead of stupid "500 Server-side" error) you can use this:

Options -Indexes +FollowSymlinks

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]

The rule above will check if target file exists BEFORE making rewrite .. so if /test.php does not exist, no rewrite will occur.

Upvotes: 4

Related Questions