?
Solved

redirect if 404 (Apache/php)

Posted on 2010-01-12
20
Medium Priority
?
1,165 Views
Last Modified: 2013-12-12
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
Comment
Question by:Luis Clara Fernandes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
  • 2
  • +2
20 Comments
 
LVL 14

Assisted Solution

by:Kalpan
Kalpan earned 400 total points
ID: 26291534
@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
 
LVL 14

Assisted Solution

by:Kalpan
Kalpan earned 400 total points
ID: 26291563
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
 
LVL 11

Accepted Solution

by:
sompol_kiatkamolchai earned 400 total points
ID: 26291642
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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 7

Expert Comment

by:frankky
ID: 26294296
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
 
LVL 5

Author Comment

by:Luis Clara Fernandes
ID: 26296328
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
 
LVL 7

Assisted Solution

by:frankky
frankky earned 400 total points
ID: 26296406
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
 
LVL 5

Author Comment

by:Luis Clara Fernandes
ID: 26296445
of course
0
 
LVL 5

Author Comment

by:Luis Clara Fernandes
ID: 26420654
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
 
LVL 57

Assisted Solution

by:giltjr
giltjr earned 800 total points
ID: 26530689
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
 
LVL 5

Author Comment

by:Luis Clara Fernandes
ID: 26532417
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
 
LVL 57

Expert Comment

by:giltjr
ID: 26533498
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
 
LVL 5

Author Comment

by:Luis Clara Fernandes
ID: 26543491
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
 
LVL 5

Author Comment

by:Luis Clara Fernandes
ID: 26543512
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
 
LVL 57

Assisted Solution

by:giltjr
giltjr earned 800 total points
ID: 26546049
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
 
LVL 5

Author Comment

by:Luis Clara Fernandes
ID: 26549582
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
 
LVL 5

Author Closing Comment

by:Luis Clara Fernandes
ID: 31675995
Thankyou guys
0
 
LVL 57

Expert Comment

by:giltjr
ID: 26549973
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
 
LVL 5

Author Comment

by:Luis Clara Fernandes
ID: 26550144
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

Limited time offer using promo code EXPERTS25

Designed with a wealth of functionality and convenience, ATEN's new Thunderbolt™ 2 Sharing Switch takes your Thunderbolt setup to the next level. Now through August 31, 2017, Experts Exchange members get 25% off the US7220 on the ATEN USA eShop using promo code EXPERTS25.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question