简体   繁体   中英

404 Error Page in .htaccess

So I am trying to add custom error page to my .htaccess file. The .htaccess file is in the website root. This is the only way it seems to work in sub-directories (eg https://www.example.com/nope/nothinghere.html ):

ErrorDocument 404 https://www.example.com/404.html

Now it doesn't seem to register as a 404 error page but just seems to temporarily redirect a missing page to 404.html. You can see it in the URL as well, it changes to 404.html.

So I try this:

ErrorDocument 404 /404.html

This seems to register as a 404 error page but it doesn't work with sub-directories. What happens is it is looking for all of the 404.html resources in a missing subdirectory and then the page looks like a mess. Any suggestions?

Since you have tagged this item with PHP. My guess is you are using PHP. As such, my suggestion would be to set error page to 404.php page where you can both dictate the content of the page as well as the response code (via header() call).

I'm a dummy. It was a matter of changing relative links to absolute links

you'll have to use the below script in your htaccess file -

ErrorDocument 404 https://www.example.com/404.html

You have to provide the absolute 404 URL to load the 404 page from within any subdirectory as well. So even when you try to load an unknown URL like https://www.example.com/xyz/index.html , it redirects to the absolute URL https://www.example.com/404.html

Very common issue when dealing with pages removed after Google has cached and indexed.

Hope this helps.

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