开发者

Apache .htaccess 404 error redirect

开发者 https://www.devze.com 2023-02-14 16:10 出处:网络
I\'m trying to make an htaccess file that will redirect all 404 errors through index.php. I would like the page the user failed to access to be appended to index.php\'s URL so that I can find out what

I'm trying to make an htaccess file that will redirect all 404 errors through index.php. I would like the page the user failed to access to be appended to index.php's URL so that I can find out what page request failed.

For example, if they tried to access http://example.com/doesntexist.php, apache should redirect them to http://example.com/index.php/d开发者_StackOverflow中文版oesntexist

This is what I have:

RewriteCond %{REQUEST_FILENAME}  !-l
RewriteCond %{REQUEST_FILENAME}  !-d
RewriteCond %{REQUEST_FILENAME}  !-f
RewriteRule .* index.php?%{QUERY_STRING} [L]

This works on one of my webhosts but failed when I transfered the site to another webhost (GoDaddy).

Thanks guys


I can't test this right now, but according to this answer, when you set a

ErrorDocument /404.php

(or index.php, doesn't matter)

The $_SERVER["REQUEST_URI"] variable will contain the original, failed request.


A side note from the direct solution, as this prevented the proper ErrorDocument redirection to work. Hope this serves as a reference for anyone which happened to stumbled on the same error as mine.

I have created an .htaccess and put

ErrorDocument 404 /Error/404.html

But the redirect never work and the default Not Found page was shown instead.
The problem is actually lies in the setting in apache2.conf (or httpd.conf).

Removing line below immediately solved the problem

AllowOverride None

It prohibits the .htaccess from overriding the default setting.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号