I'm quite new to PHP and htaccess. I'm trying to use php-mvc framework for my project. However, they don't support custom error page. So, I'm trying to implemented it for my project, since I need it in my project.
I have a problem with htaccess displaying custom error page when http response is 404. I've already checked all the answers regarding ErrorDocument and RewriteEngine on stackoverflow, none of the answers work for me. Here is my htaccess code:
Options -Indexes
RewriteEngine On
RewriteBase /myproject/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
ErrorDocument 404 /myproject/public/404.html?page=error-404
If I remove RewriteEngine On
, it works. I think ErrorDocument is conflicting with RewriteEngine.
Any help would be appreciated. Cheers.
The problem is that these conditions:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
Essentially say: If the request isn't for a file, directory or symbolic link that exists. So, essentially what would serve a 404.
So there will never be an apache generated 404 in this instance because any request that would normally generate one simply gets captured by the index.php
of your application. That means in order to return the 404.html
page, you need to link to it from your php application. You need to write php code that recognizes when the $_GET['url']
doesn't point to a valid URL and redirects to the /myproject/public/404.html?page=error-404
instead.
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.