Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess not redirecting to 404.php instead shows page name

Am trying to make a custom 404 page for my website and am having .htaccess file in the root directory where am using this rule

ErrorDocument 404 404.php //I want to redirect to 404

So when I change a valid file name like home.php to home1.php it doesn't redirect me instead it echo's 404.php on that page

Side Note: 404.php is in the root directory only

like image 846
Random Guy Avatar asked Apr 04 '13 18:04

Random Guy


4 Answers

In my case, using an Ubuntu distribution, the directive ErrorDocument has no effect if it is in the .htaccess in htdocs directory or elsewhere: it turned out that it should be put in the proper /etc/apache/sites-enabled/*.conf file, inside the <VirtualServer> directive (for example, if the website is providing https pages, inside the directive <VirtualHost *:443>).

like image 107
marcocassisa Avatar answered Oct 30 '22 10:10

marcocassisa


This should do it

RewriteEngine on
ErrorDocument 404 http://www.yoursite.com/404.php
like image 25
Kevin Lynch Avatar answered Nov 14 '22 09:11

Kevin Lynch


In your .htaccess file, you should be able to use:

RewriteEngine on
ErrorDocument 404 /404.php

You can set additional error documents using this method, but I'd put them in a separate errors directory:

ErrorDocument 400 /errors/400.php
ErrorDocument 401 /errors/401.php
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php
like image 8
nickhar Avatar answered Nov 14 '22 09:11

nickhar


you could do the following to 404 old pages with your htaccess

RewriteEngine on
ErrorDocument 404 /404.php

but i would personally recommend

RewriteEngine on
RewriteRule home.php /home1.php [R=301,L]

as this would do a 301 redirect from the old page name to the new page name, so any cached search engine results would still end up at the correct page instead of hitting a 404

like image 4
bizzehdee Avatar answered Nov 14 '22 11:11

bizzehdee