.htaccess "Internal Server Error" when adding / to URL

Viewed 238

I've got the following .htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^profile/([A-Za-z]+) profile.php [NC,L] 

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

But you can't add a / to the end of the URL. For example if you goto something it works but something/ doesn't. So how do I allow a / to be added to the URL?

2 Answers

Here is a possible solution if anyone else is having the same problem (not fully tested but seems to work).

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^profile/([A-Za-z]+) profile.php [NC,L] 

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

To allow the trailing slash in URLs that point to your php files, replace your last rule with this :

RewriteRule ^(.*)/?$ $1.php [NC,L]

/? matches the slash as an optional character so your rule will also match URIs not ending with a / .

Related