.htaccess show 404 page with rewrite?

笑着哭i 提交于 2019-12-23 04:24:26

问题


I have a .htaccess file that essentially "removes" the file extension in the web browser (from example.php to example). Here's the code:

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

Basically, if the file (about) isn't found, it adds the .php extension (about.php). I want to (if the file still isn't found) redirect the user to 404.php. So if the user loads /page1, and page1.php doesn't exist, it redirects to the 404 page. I added this code to the .htaccess:

ErrorDocument 404 /404.php

On it's own, this code will redirect to the 404 page. But when I have the entire code...

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

ErrorDocument 404 /CWTC/404.php

...it just shows a "500 Internal Server Error".

Does anyone know what I'm doing wrong?

Thanks!


回答1:


This has solved my problem:

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

ErrorDocument 404 /404.php

Thanks!



来源:https://stackoverflow.com/questions/13532119/htaccess-show-404-page-with-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!