Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I recently upgraded a site and almost all URLs have changed. I have redirected all of them (or so I hope) but it may be possible that some of them have slipped by me. Is there a way to somehow catch all invalid URLs and send the user to a certain page and somehow know what URL the person came from so I could log this, and fix those? I'm thinking I could use .htaccess somehow but am not sure how. I am using PHP Thanks so much!

error file is already in .htaccess but seems nothing going to change you can see the error file as below

AddHandler application/x-httpd-php5s .php
ErrorDocument 404 /content/404.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

to see the problem what i want result is i need some thing to redirect if some one types wrong url just check the link even if you delete half of the location at the endof url it still opens http://adsbuz.com/classifieds/abu-dhabi-uae/

it sholud go to the error page but its not giong i neeed some kind force to push it to error page thanks

share|improve this question
    
If it's not going to the error page, where is it going instead? What do your logs say? –  Jenny D Aug 31 '12 at 10:54
    
thanks for your reply quanta i was busy some where else so please sorry for delay –  david Sep 3 '12 at 18:46
    
thanks for your reply quanta i was busy some where else so please sorry for delay ok now come to the point here are 2 different url one the first one is old one which i edited and the secound one is edited one #1 old one (which is no longer the server) adsbuz.com/vehicles-cars/toyoya/… #2 the editet one adsbuz.com/vehicles-cars-for-sale/toyoya/… i need only the secound one with the vehicles-cars-for-sale as you can see after the site name adsbuz in both and both are opening thanks –  david Sep 3 '12 at 18:55

1 Answer 1

You can create a custom 404 file. Assuming you are using Apache you would add the following line to your .htaccess

ErrorDocument 404 /errorfilename.php

This code basically tells the server that if a user encounters a 404(Page not Found) Error to display errorfilename.php.

In this PHP file you can add either code to email you when a user gets to the 404 page or add other logic to support

  • Database storage
  • Writing to a file/custom log
  • searching a database for the file location and redirecting the user to the new destination

In PHP to get the location of the 404 page you would use the following global variable

$_SERVER['HTTP_REFERER']

Example of your 404.php page

<?php
$referer = $_SERVER['HTTP_REFERER'];
//this is where you would either email yourself the $referer, 
//insert it into a database or perform more complex operations.
?>
<html>
<head>
<title>404</title>
<body>
Sorry, the page you are looking for has been moved/deleted.<br>
The system administration has been notified of this error.<br>
<a href="index.php">Go to Homepage</a>
</body>
</html>
share|improve this answer
    
thganks for your reply its the same .htaccess already in my .htaccess file but not work what i am asking for –  david Aug 31 '12 at 3:38
    
thanks Bart can you please make it more clear i really appreciate still learning –  david Sep 3 '12 at 21:32
    
there is already ErrorDocument 404 /content/404.php as you can see in .htaccess file so whats the difference i don't understand can you please explain it little bit thanks for your response –  david Sep 6 '12 at 11:35
    
there is already ErrorDocument 404 /content/404.php as you can see in .htaccess file so whats the difference i don't understand can you please explain it little bit thanks for your response –  david Sep 6 '12 at 11:36
    
forget about anything else i just want to redirect whole directory to new directory thanks –  david Sep 6 '12 at 11:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.