Mobile app version of vmapp.org
Login or Join
Turnbaugh106

: Have Apache show 404 at missing page URL instead of redirecting to error page URL Assuming non-existent-page.html does not exist, and the user is trying to access that page and triggered a 404

@Turnbaugh106

Posted in: #Apache #Htaccess #ModRewrite #UrlRewriting

Assuming non-existent-page.html does not exist, and the user is trying to access that page and triggered a 404 error.

Can i show the requested page URL:
www.example.com/non-existent-page.html

instead of the error page URL:
www.example.com/404.html

Solution:

While looking at Stephen Ostermiller's answer i knew i was using a relative URL, but i realized it had a missing trailing slash at the end because i was pointing to a directory and not a page. This mostly occurs with some xSP with bad configurations.

Problem

ErrorDocument 404 /error/404 <-- no slash


Fix

ErrorDocument 404 /error/404/ <-- added slash

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Turnbaugh106

2 Comments

Sorted by latest first Latest Oldest Best

 

@BetL925

Apache server can be configured to show the error page at the error URL, or it can redirect to the error page. It is almost better to show the error page directly at the URL rather than redirecting to it.

The Apache ErrorDocument directive explains how to implement it both ways:


URLs can begin with a slash (/) for local web-paths (relative to the DocumentRoot), or be a full URL which the client can resolve.


In practical terms, that means if you specify the error document as an absolute URL it will cause a redirect to the error page:

ErrorDocument 404 www.example.com/404.html

but if you specify the error document as a relative URL starting with a slash, it will show the error document at the original URL where the error occurred:

ErrorDocument 404 /404.html


My guess is that you have your ErrorDocument directive configured as an absolute URL either in your .htaccess file or your httpd.conf file. You need to edit it to change it to a relative URL.

10% popularity Vote Up Vote Down


 

@Michele947

You need to use the RewriteEngine for this, adding a condition/rule to detect if the HTTP request is ending with the html extension, then redirect to the same URL without it, in .htaccess:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET (.*).html HTTP
RewriteRule (.*).html$ [R=301]


This will redirect each request ending in .html version to the same url without the extension. If the non .html version exist, such when accessing a bad URL, the 404 page will be shown.

Take a look at the RewriteRule directive here

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme