• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1173
  • Last Modified:

redirect if 404 (Apache/php)

I want to redirect not found pages to index.php.

how do i send the user to index page if they get a 404 error?

I'm already using some rewrite rules to improve SEO like you could see in joigned file.
htaccess.txt
0
Luis Clara Fernandes
Asked:
Luis Clara Fernandes
  • 9
  • 4
  • 2
  • +2
6 Solutions
 
KalpanCommented:
@clarafer

You need to create .htaccess file in you apache webserver root directory with following...

ErrorDocument 404 /errors/404error.php

Please refer

http://bobpeers.com/technical/404_redirection

hope this will be helpful...

thanks

Kalpan

0
 
KalpanCommented:
This will be also helpful...

Utilizing Apache's built in custom error document handling you can setup your own error 404 document that redirects the user to your index page and gives them a message as well as displays your index page. The first thing we need to do is build our error handler. I use a php script that includes my index.php file, gives the user a message at the top and the re-directs them to the main page after 2 seconds. Here's how:

<?php
//errror404.php
echo "<font color="#FFFF00" size="3"><center><strong>You have reached a page that has changed or no longer exists.
      You have been redirected to our index page. Please update your bookmarks!</strong></center></font>";
//Meta refresh to send them back to index.php
echo "<meta http-equiv="refresh" content="2;URL=/index.php">";
//include the index.php so that we can give them something to look at besides an ugly error
include 'index.php';
?>

There you have it! A simple php script that gives you a good looking Error 404 page and redirects the user to your index page. On the next page, we'll show you how to setup a htaccess file that allows Apache to use this new error404.php script.

The next step requires you to either have administrative access to your Apache web server's configuration files or send an email to your admin and ask them to allow overrides for your website. A directive should be added to your httpd.conf for the directory which your website lives in. Let's say for this example it's /www/htdocs. Here's a line in your httpd.conf that you must have for this .htaccess file to work:

<Directory /www/htdocs/yoursite>
     AllowOverride FileInfo
</Directory>

Once you have that done, you'll need to create a file called ".htaccess" and place it inside your website's directory. Here's how simple this .htaccess file is:

ErrorDocument 404 /error404.php
0
 
sompol_kiatkamolchaiCommented:
Hi clarafer,

You can use ErrorDocument directive.

Here is reference.
http://httpd.apache.org/docs/2.2/mod/core.html#errordocument

Hope this help,
Sompol
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
frankkyCommented:
As far as going Joomla specific, you can look at :

http://forum.joomla.org/viewtopic.php?p=1973770

if you really want only to point to the homepage then in .htaccess just write:

ErrorDocument 404 /index.php
0
 
Luis Clara FernandesIAM CoordinatorAuthor Commented:
Hello,
 
Thankyou guys for all your answers.
I tried already to setup ErrorDocument 404 /index.php in my .htaccess file, but nothing happends. My website is hosted at hostgator, do you think I couldn't setup Error pages in htaccess ? In Cpanel I could setup error pages, but it doesn't work to.
Thankyou
0
 
frankkyCommented:
Have you renamed the default htaccess.txt to .htaccess?

Here's a link to hostgator tutorial to create custom error pages using CP:

http://www.hostgator.com/tutorials/xskincpanel/cpanel_xskin_errorpages.htm

0
 
Luis Clara FernandesIAM CoordinatorAuthor Commented:
of course
0
 
Luis Clara FernandesIAM CoordinatorAuthor Commented:
Hello every body,

Finally I got many answers but nobody answers to my question. Whatever, my case is quite simple:

I have many urls indexed in Google. But I changed the structure of the website and don't care about old pages. But I want to redirect all visitors when a page indexed by Google is not found.

mywebsite.com/index.html exist -> nothing to do
mywebsite.com/anysubdirectory/*.html exist -> nothing to do
mywebsite.com/anysubdirectory/*.html doesn't exist -> redirect to root page

That's all.

Somebody could help me to rewrite the rules in joigned .htaccess file or explain me a good way to do that?

Thankyou
0
 
giltjrCommented:
Have you gotten your .htaccess file to work yet? If you only want to redirect on 404's then using Apaches error message is the best and easiest way.  If you can't get .httaccess to work, then nothing you do will help.
0
 
Luis Clara FernandesIAM CoordinatorAuthor Commented:
Hello,

I had  ErrorDocument 404 /index.php in one of my websites where I'm not using rewrite rules and it works fine.

But where I'm using the rewrite rules that you find in the htaccess.txt joigned to the question, it doesn't work.

Take a look at: http://www.mycarvsyours.com/directory

The path directory doesn't exist and I get an error page and no redirection.

Thank you for your help


0
 
giltjrCommented:
Where in the file did you have the ErrorDocument, beginning or end?

I'll have to read and see if I can find what order the statements are processed, but I beleive that you would need it at the end.
0
 
Luis Clara FernandesIAM CoordinatorAuthor Commented:
Hello,

I tried both, but it doesn't work.

Joomla rewrite rules send error 404 to a page that shows Component not found. I guess that this rewrites rules used by joomla send visitor to this error page, I was thinking that I could change this rule to redirect visitor to index.php.

What do you think?


0
 
Luis Clara FernandesIAM CoordinatorAuthor Commented:
There's the rewrite rules I'm using and my try  without ErrorDocument:

#Joomla rewrite rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
RewriteRule (.*) index.php


#My try to redirect visitor in case of error 4 (not working)
#writeCond %{HTTP_HOST} ^mycarvsyours\.com/.*$
#RewriteRule (.*) http://www.mycarvsyours.com/.*$1 [R=404,L]
#RewriteRule ^$ http://www.mycarvsyours.com/index.php [L,R=404]

Open in new window

0
 
giltjrCommented:
One issue you may have is that REQUEST_FILENAME has been known to "act weird".   You may need to enable rewrite log.  Depending on where (inside or outside of a directory statement) will depend on if the file is properly searched for.  You may want to read:

     http://marc-abramowitz.com/archives/2005/12/18/whats-up-with-request_filename-in-mod_rewrite/

However using:

ErrorDocument 404 /index.php

Should work "faster", meaning it has less overhead than a bunch of rewrites.
0
 
Luis Clara FernandesIAM CoordinatorAuthor Commented:
Hello,

That's the best way. But I discovered that Joomla overrides the htaccess file when it comes to 404 errors.

Take a look at: http://forum.joomla.org/viewtopic.php?f=428&t=282063

This is a bad solution that works, but show the 404 error before redirection. Somebody understand how Joomla overrides htaccess?  
0
 
Luis Clara FernandesIAM CoordinatorAuthor Commented:
Thankyou guys
0
 
giltjrCommented:
Thanks for the points.  Although I saw Joomla, it never hit me until now.  Have you tried reading either of the following.

http://docs.joomla.org/Tutorial:Create_a_Custom_404_Error_Page

http://forum.joomla.org/viewtopic.php?f=199&t=251089
0
 
Luis Clara FernandesIAM CoordinatorAuthor Commented:
You did it! Thank you so much. I' work on voip (cisco & asterisk), if you have any question about, it will be my pleasure.

0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 9
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now