1

My .htaccess file causes an internal error when I add a slash to a php file in the folder that the .htaccess file is in. Why does this happen and how could I prevent this error from happening?

I would like the page where the internal error is being caused to display the php file (instead of a redirect as some users have the php file with a / at the end cached)

Here's my .htaccess file:

RewriteEngine on
RewriteBase %{REQUEST_URI} ^(.*)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
ErrorDocument 404 /content/errors/404.php
ErrorDocument 403 /content/errors/403.php
ErrorDocument 500 /content/errors/500.php

Examples:

https://mywebsite.com/folder/file ✔️ works!

https://mywebsite.com/folder/file.php ✔️ works!

https://mywebsite.com/folder/file/ ❌ 500 internal error

MysticMtn
  • 11
  • 3
  • Apache views `file/` as a directory request. Add `RewriteCond %{REQUEST_FILENAME} !-d` to include it in the rewrite – Phil Jul 12 '19 at 03:02
  • @Phil thanks for your comment! Very much appreciated, BUT adding `RewriteCond %{REQUEST_FILENAME} !-d` still did not fix my problem, the 500 internal error still persists. – MysticMtn Jul 12 '19 at 03:07
  • Ah right, you'd need some way to capture the request filename prior to the trailing `/` to check for the existing `.php` file. I'm sure this has come up before, will do a bit of a search – Phil Jul 12 '19 at 03:11
  • How would I do so? I'm not that familiar with .htaccess – MysticMtn Jul 12 '19 at 03:12
  • Check out [this answer](https://stackoverflow.com/a/21417551/283366). They first rewrite requests for non-existent directories to the same URL, minus the trailing slash. From there, your existing rules should work – Phil Jul 12 '19 at 03:13
  • [This answer](https://stackoverflow.com/a/36340272/283366) might also help – Phil Jul 12 '19 at 03:16
  • @Phil the last answer you linked me to worked flawlessly! Thank you! Also could you post this as an answer so I can give you some credit? – MysticMtn Jul 12 '19 at 03:23
  • Give credit to the [question](https://stackoverflow.com/q/36340231/283366) and answer that helped – Phil Jul 12 '19 at 03:32

0 Answers0