How To Redirect 404 Error To Page Using .htaccess
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Rewrite URL after redirecting 404 error htaccess up vote 11 down vote favorite 11 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://domain.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://domain.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://domain.com/400/ http://domain.com/500/ etc.) Any ideas? .htaccess redirect url-rewriting rewrite custom-error-pages share|improve this question asked Nov 13 '13 at 19:36 Leinardo Smtih 2621313 add a comment| 4 Answers 4 active oldest votes up vote 17 down vote accepted 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 exi
404 Error Htaccess Protect Folder Password Management Password Protection Uncategorized Website Security Htaccess Posts Password Protect a Directory How to Stop Directory Listing Change the Default Directory Page Deny a user by IP Address Force users to use the WWW or Non-WWW version of your domain Htaccess Share How to Redirect your 404 error to a Custom Page A 404 error message is the standard HTTP standard response code which is returned when the visitor cannot communicate with the server. Visitor gets a standardized undesirable 404 Error Page This is a very common error on the web and it occurs when you are trying to visit a page which has either been deleted or has been moved http://stackoverflow.com/questions/19962787/rewrite-url-after-redirecting-404-error-htaccess somewhere else. For example, if you change the structure of your website and move a certain directory to a different part of your site, anyone trying to visit the old page url will get a 404 error message. 404 Error Messages A 404 error is pretty much lost traffic with in your website. You have managed to get the visitor there with your SEO efforts but a changed page over site results in the visitor http://www.htaccessbasics.com/404-custom-error-page/ getting the generic server 404 error messages. A lot of standard 404 messages are useless and do not even refer your visitor back to the homepage. A 404 error message usually looks something like this : Not Found The requested URL /index.php was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.7a mod_bwlimited/1.4 PHP/5.2.6 Server at yourwebsite.com Port 80 Example of Windows Internet Explorer 10 404 Error Message - 404 Error Page 404 Error Message Reality If a visitor comes to your site and sees a standard 404 error message it's unlikely they will make the effort to see any part of your site. Therefore it is very important to create a 404 page on your site and redirect traffic from incorrect urls. Thankfully, htaccess makes this very easy. First of all you need to create a 404 error page. So for example, you would create a page at http://www.yoursite.com/404.php which says something like : It appears you are looking for something which isn't there. Either you have entered an incorrect URL or we have messed up. Why not visit our home page or alternatively, search for what you are looking for in the search box below. Whilst a 404 error page does not sen
file enables you to redirect users from an old page to a new page without having to keep the old page. For example, if https://help.dreamhost.com/hc/en-us/articles/215747748-How-can-I-redirect-and-rewrite-my-URLs-with-an-htaccess-file- you use index.html as your index file and then later rename index.html to home.html, you could set up a redirect to send users from index.html to home.html. For example: Redirect /path/to/old/file/old.html http://www.example.com/new/file/new.html The first path to the old file must be a local UNIX path, NOT the full path. So, if the .htaccess file is in the how to directory /example.com, you would not include /home/exampleuser/example.com in the local UNIX path. The first / represents the example.com directory. If the old file was in that directory, you would follow the / with the old file name. The second path to the new file can be a local UNIX path, but can also be a full URL to how to redirect link to a page on a different server or the same server. Here are a few examples of some redirects: Redirect /index.html /new/ Redirect /index.html /default.html Redirect /private/ http://www.example.com/private/ Redirect /img/logo.gif http://www.example.com/images/logo.gif Another form of redirection uses the RedirectMatch command: RedirectMatch "^/oldfile\.html/?$" "http://example.com/newfile.php" You can also redirect 404 errors. Instead of throwing a 404 page, this redirects to the homepage of the website. ErrorDocument 404 http://example.com/ Redirect non-existing pages to index.php If a visitor attempts to access a page that doesn't exist, they are presented with a 404 error. You can instead redirect any request to a non-existing page to your index.php file (or any index file) by adding the following code in your .htaccess: Options +SymLinksIfOwnerMatch RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] If your index page isn't index.php, just change the last line to your actual index file. Then the visitor is redirected back to your home page. Automatically loading a subdirectory This example redirects the ROOT domain's URL to any subdirectory. In this example, it