简体   繁体   中英

.htaccess Internal Server Error: ErrorDocument not working (RewriteEngine on)

Whenever I have RewriteEngine on my ErrorDocument - part is not working. This is my current .htacces file:

ErrorDocument 404 404.php

Options -MultiViews
RewriteEngine On
RewriteBase /

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

So when I open a not existing file in my browser, I get a 500 Internal Server Error and I am not getting redirected to the 404.php - file.

Please help me ;)

This is because you're blindly rewriting the .php to the end of the URI. Let's take a look to see what happens when you go to a 404 URL:

  • you request: http://example.com/blahblah
  • The URI is not a file (passes !-f )
  • The URI is not a directory (passes !-d )
  • The URI matches (.*) .
  • The URI gets rewritten to include a .php
  • The URI is now /blahblah.php
  • The URI is not a file (passes !-f )
  • The URI is not a directory (passes !-d )
  • The URI matches (.*) .
  • The URI gets rewritten to include a .php
  • The URI is now /blahblah.php.php
  • The URI is not a file (passes !-f )
  • The URI is not a directory (passes !-d )
  • The URI matches (.*) .
  • The URI gets rewritten to include a .php
  • The URI is now /blahblah.php.php.php

etc.

You need to make sure what you're rewriting to actually exists or you cause a loop:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM