简体   繁体   中英

Apache .htaccess: ErrorDocument and RewriteEngine not working together

Please take a look at the following .htaccess

ErrorDocument 404 /404/

RewriteEngine On
RewriteRule (.*) index.php [L]

With this setup, I am using header('HTTP/1.1 404 Not Found'); in PHP to redirect to the error handling page and send the appropriate HTTP status code. The correct 404 status code is sent, but the browser shows a blank page and the access log shows "GET /invalid-url/ HTTP/1.1" 404 -

Can anyone tell me how to make ErrorDocument work with Apache URL rewrites?

Yes declare 404 document after the Rewrite rules

It's normal your server do not push any 404 error, you're using a catchall regexp ((.*)) as the only rewrite rule.

But your issue is not really htaccess related. In php if you send 404 header the browser wont be redirected to the 404 page automaticaly, but you have to serve error page content yourself in PHP, as it is done by most frameworks with internal routing system.

I guess the answer to my question is: I don't need to. What I was trying to accomplish was a "404 redirect". That is, when requesting an invalid URL, redirect to the 404 document along with setting the "Status 404 Not found" header. And I am not sure that's something I want, because invalid URLs should be marked with the 404 status code, not redirected.

如果在ErrorDocument 之前定义Rewrite 它应该可以工作。

Try this


ErrorDocument 404 /404.html

RewriteEngine On
RewriteRule (.*) index.php [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