Computer Geeks

Computer Geeks

Geek Shop

Geek News

Geek Stuff

Science Geek

Computer Gaming

Linux Chat

Building Websites

Computer Forums

Computer Help Forum

Computer Hardware Forum

Computer Software Programs


Go Back   Computer Forums > Geek Stuff

Geek Stuff General forum for discussing anything geek. If your topic doesn't fit in any of the other forums then post it here.

Computer Geeks
» Active Discussions
Computer Geeks
No Threads to Display.
» Other Websites
- Software Publishing

- Server Hardening
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #7  
Old 04-19-2006, 05:40 AM
Coop Coop is offline
Member
GB Beginner
 
Join Date: Apr 2006
Posts: 34
Default

If you really want to use php, there are several ways to do it. The simplist is with a straight 301 redirect with

Quote:

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.domain.org/index.php");
?>
Obviously replacing http://www.domain.org/index.php with the URL you want to redirect to. To use this method, the file must replace the original file you are wanting to redirect from.


A more flexible method is to use a map. The advantage of this is that it has a default URL that redirects, typically, to your home page (prevents loss of traffic when pages are moved or deleted). In addition, the map allows you to effectively redirect from multiple URL's to there new location.

Code:
<?php
$map = array(
'/old/1.html' => '/new/2.html',
'/old/2.php' => '/newlocation/3.html');

if (isset($map[$_SERVER['REDIRECT_URL']])) {
    $new_loc = 'http://' .
               $_SERVER['HTTP_HOST'] .
               $map[$_SERVER['REDIRECT_URL']];
    if (isset($_SERVER['REDIRECT_QUERY_STRING'])) {
        $new_loc .= '?' .
                    $_SERVER['REDIRECT_QUERY_STRING'];
    }
    header("Location: $new_loc");
} else {
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: ".$_SERVER['HTTP_HOST']);
}
?>

You should save this as error.php in the root directory of your site, and then add an .htaccess file with the following directive :

ErrorDocument 404 /error.php

That is just to handle 404 errors, you can also add additional error codes if you wish. With this script, if there is no map for the missing page, then you get 301 redirected to the home page of the site. However, if the missing page is one of the pages mapped in the map array, the script will 301 redirect you to that page instead.

These scripts are most useful when the new loocation on your site does not mirror the structure of the original location.

HTH
__________________
Coop
Reply With Quote
 

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Powered by vBadvanced CMPS v3.2.3

All times are GMT -5. The time now is 01:59 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
HTML Help provided by HTML Help Central.