Custom 404 error issues with Apache - the ErrorDocument is 404 as well

余生长醉 提交于 2019-12-17 10:54:13

问题


I am trying to create a custom 404 error for my website. I am testing this out using XAMPP on Windows.

My directory structure is as follows:

error\404page.html
index.php
.htaccess

The content of my .htaccess file is:

ErrorDocument 404 error\404page.html

This produces the following result:

However this is not working - is it something to do with the way the slashes are or how I should be referencing the error document?

site site documents reside in a in a sub folder of the web root if that makes any difference to how I should reference?

When I change the file to be

ErrorDocument 404 /error/404page.html

I receive the following error message which isn't what is inside the html file I have linked - but it is different to what is listed above:


回答1:


The ErrorDocument directive, when supplied a local URL path, expects the path to be fully qualified from the DocumentRoot. In your case, this means that the actual path to the ErrorDocument is

ErrorDocument 404 /JinPortfolio/error/404page.html

When you corrected it in your second try, the reason you see that page instead is because http://localhost/error/404page.html doesn't exist, hence the bit about there being a 404 error in locating the error handling document.




回答2:


.htaccess files are disabled by default in Apache these days, due to performance issues. When .htaccess files are enabled, the web server must check for it on every hit in every subdirectory from where it resides.

Just figured it was important to note. If you want to turn on .htaccess files anyway, here's a link that explains it:

http://www.tildemark.com/enable-htaccess-on-apache/




回答3:


Instead of adding them to your .htaccess file, you can add them to any of your virtual host files.

<VirtualHost *:80>

    DocumentRoot /var/www/mywebsite

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    ErrorDocument 404 /error-pages/404.html
    ErrorDocument 500 /error-pages/500.html
    ErrorDocument 503 /error-pages/503.html
    ErrorDocument 504 /error-pages/504.html

</VirtualHost>

Where error-pages is a subfolder in the mywebsite folder, containing the custom error pages. Make sure you restart your apache to view your changes.

$sudo /etc/init.d/apache2 reload



回答4:


Whatever url you enter as the default 404 page must be either absolute or relative from the root folder : That's why some make the mistake of treating its url like the rewrite engines' url which is relative from the folder where .htaccess is located.



来源:https://stackoverflow.com/questions/3397868/custom-404-error-issues-with-apache-the-errordocument-is-404-as-well

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