52

So I know this may seem a little strange but I for sake of consistency, I would like all my urls to appear in this form:

http://example.com/page/ 

So far I have gotten the regular pages working but I cannot seem to get the error pages working properly.

If the user visits a page or directory that does not exist, I would like the browser to hard redirect to:

http://example.com/404/ 

This directory, however, will not actually exist. The real location of the error page will be under

/pages/errors/404.php

Also, although I do not need an exact answer for all the various errors (400, 401, 403, 404, 500), I will be applying whatever method is given to redirect all of these to their "proper" URL's eg.

http://example.com/400/ 
http://example.com/500/ 

etc.

Any ideas?

Vega
  • 27,856
  • 27
  • 95
  • 103
godismyjudge95
  • 922
  • 1
  • 9
  • 15

4 Answers4

93

Try this in your .htaccess:

.htaccess

ErrorDocument 404 http://example.com/404/
ErrorDocument 500 http://example.com/500/
# or map them to one error document:
# ErrorDocument 404 /pages/errors/error_redirect.php
# ErrorDocument 500 /pages/errors/error_redirect.php

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]

RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]

# or map them to one error document:
#RewriteCond %{REQUEST_URI} ^/404/$ [OR]
#RewriteCond %{REQUEST_URI} ^/500/$
#RewriteRule ^(.*)$ /pages/errors/error_redirect.php [L]

The ErrorDocument redirects all 404s to a specific URL, all 500s to another url (replace with your domain).

The Rewrite rules map that URL to your actual 404.php script. The RewriteCond regular expressions can be made more generic if you want, but I think you have to explicitly define all ErrorDocument codes you want to override.

Local Redirect:

Change .htaccess ErrorDocument to a file that exists (must exist, or you'll get an error):

ErrorDocument 404 /pages/errors/404_redirect.php

404_redirect.php

<?php
   header('Location: /404/');
   exit;
?>

Redirect based on error number

Looks like you'll need to specify an ErrorDocument line in .htaccess for every error you want to redirect (see: Apache ErrorDocument and Apache Custom Error). The .htaccess example above has multiple examples in it. You can use the following as the generic redirect script to replace 404_redirect.php above.

error_redirect.php

<?php
   $error_url = $_SERVER["REDIRECT_STATUS"] . '/';
   $error_path = $error_url . '.php';

   if ( ! file_exists($error_path)) {
      // this is the default error if a specific error page is not found
      $error_url = '404/';
   }

   header('Location: ' . $error_url);
   exit;
?>
godismyjudge95
  • 922
  • 1
  • 9
  • 15
Jay
  • 2,123
  • 1
  • 22
  • 29
  • Exactly what I am looking for... but is there anyway to use a relative path on the ErrorDocument declaration? – godismyjudge95 Nov 14 '13 at 00:03
  • If you use a relative path, you need to point to an existing file, and that file can then do the redirect to the non-existant /404/ url. Editing my answer to show this now... – Jay Nov 15 '13 at 00:29
  • Could I have just one file that redirects for any error? By using a url query or something? – godismyjudge95 Nov 15 '13 at 03:07
  • @LeinardoSmtih - I updated again to show more examples. Hope that helps! – Jay Nov 15 '13 at 07:34
25

Put this code in your .htaccess file

RewriteEngine On
ErrorDocument 404 /404.php

where 404.php is the file name and placed at root. You can put full path over here.

Ashwani Panwar
  • 3,819
  • 3
  • 46
  • 66
  • 2
    It's working but I have one issue if I use http://example.com/wrongname then I am getting 404-page content but the URL not changing. I mean I am getting a 404 pages but the URL is still showing http://example.com/wrongname. It should be http://example.com/404. right? – user9437856 Nov 02 '20 at 12:36
7

Try adding this rule to the top of your htaccess:

RewriteEngine On
RewriteRule ^404/?$ /pages/errors/404.php [L]

Then under that (or any other rules that you have):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ http://domain.com/404/ [L,R]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
2

In your .htaccess file , if you are using apache you can try with

Rule for Error Page - 404

ErrorDocument 404 http://www.domain.com/notFound.html

ElMusa
  • 21
  • 1