6

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:

http://www.example.com/non-existent-page.html

instead of the error page URL:

http://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
  • Not sure what you mean, are you wanting to show a custom error document for 404 errors? Or are you wanting to "correct" the URL if the user has perhaps typed it incorrectly? – MrWhite Nov 15 '16 at 14:53
  • I already have a custom error document. What i want is - whenever the user is redirected to that error of 404 which is example.com/404 it will show the requested page URL instead which is example.com/nopage. – Explisam Nov 15 '16 at 14:56
  • It should already be showing the requested page URL (in the browsers address bar) if implemented correctly. Are you saying that you are seeing the URL of the 404 error document in the browser's address bar instead? Maybe just terminology, but the user should not be "redirected" to the error page. How have you implemented the custom error document? – MrWhite Nov 15 '16 at 15:02
  • @Explisam are you using the Apache server? – marcanuy Nov 15 '16 at 16:33
  • @marcanuy Yes, i'm. – Explisam Nov 15 '16 at 17:47
4

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 http://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.

  • I was actually using a relative URL but it wasn't pointing to a page, it was pointing to a directory without a trailing slash which had caused the problem and threw a 500 error when i reviewed the logs and redirected me to the default error page. Thank you, i had almost gone nuts figuring it out. – Explisam Nov 18 '16 at 9:27
  • 2
    for anyone ending up here with the same question and the above doesn't seem to work, check that you aren't doing other redirects in your htaccess. I had a general redirect for .php pages (so the .php wouldn't display in the url) and the way I had it configured it was 'redirecting' the error page and I was ending up with the error page url instead of the original url. To solve I added a ReWriteCond to exclude my error page from the redirect I had. RewriteCond %{REQUEST_URI} !404\.php$ [NC] – Nat Oct 2 '18 at 9:17
1

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$ $1 [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

  • 1
    I think you miss-understood my question, i'm not trying to skip the html extension, I'm trying to show the missing page URL instead of the error page URL. Please re-read my question. – Explisam Nov 17 '16 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.