Redirect to a php/html file on 404 error with .htaccess

牧云@^-^@ 提交于 2020-01-01 10:48:08

问题


I want to redirect all 404 errors to "404.php" for example.

I've tryed using ErrorDocument 404 /404.php in .htaccess, but without luck, perhaps my other rules are not allowing this somehow, I'm not really sure as I couldn't find this info on www.

If anyone have idea how to solve this, please shout :P

My current .htaccess

#### pie thingy ####
AddType text/x-component .htc

#### Rule for Error Page - 404 ####
ErrorDocument 404 /404.html

#### url rewriting ####
Options +FollowSymlinks
RewriteEngine on

#### REDIRECT IF NO SUB-DOMAIN ####
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)$ [NC]
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]

#### Remove "www." from any subdomain requests ####
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.precisehire\.com [NC]
RewriteRule (.*) http://%1.precisehire.com/$1 [R=301,L]

#### Rules for rewriting from PHP to HTML with fallback ####
RewriteCond %{REQUEST_URI} ^/(.*)\.html$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)\.html$ $1.php [nc]

回答1:


Solved by placing full URL for error document.

Example:

#### Rule for Error Page - 404 ####
ErrorDocument 404 http://www.domain.com/404.html



回答2:


It's not necessary to give the full URL. You can also mention the absolute path like

ErrorCodument 404 /pages/error.php

which is a better way of doing this than the accepted answer.



来源:https://stackoverflow.com/questions/14838488/redirect-to-a-php-html-file-on-404-error-with-htaccess

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