|
|
04-19-2006, 04:29 AM
|
Member
GB Advanced User
|
|
Join Date: Mar 2006
Posts: 60
|
|
Smooth 301 redirect php script
I need a smooth 301 redirect php script. More specifically, I am looking for a script that does the following:
Visitor goes to the page
yourSite1.com/animal/dog/1.html.
The visitor gets 301-redirected to
yourSite2.com/animal/dog/1.html.
How do I do this in php?
|
04-19-2006, 05:04 AM
|
|
Senior Member
GB GEEK
|
|
Join Date: Feb 2006
Posts: 309
|
|
Does it have to be php? You could set this up with apache using htaccess. A simple would do the trick. And it would take less than 2 minutes to set it up.
|
04-19-2006, 05:17 AM
|
Member
GB Advanced User
|
|
Join Date: Mar 2006
Posts: 60
|
|
php or .htaccess.
Well actually, I need to remap number of pages.
|
04-19-2006, 05:27 AM
|
|
Senior Member
GB GEEK
|
|
Join Date: Feb 2006
Posts: 309
|
|
Quote:
Originally Posted by yetanotherfcw
php or .htaccess.
Well actually, I need to remap number of pages.
|
I would use .htaccess, but I never tryed to redirect more than one page. So I am unsure if it will work. If you try it let me know how it works out.
|
04-19-2006, 05:43 AM
|
Member
GB Advanced User
|
|
Join Date: Mar 2006
Posts: 60
|
|
Quote:
Originally Posted by Soulwatcher
I would use .htaccess, but I never tryed to redirect more than one page. So I am unsure if it will work. If you try it let me know how it works out.
|
Ok, well, I get the feeling that it might be easier to write a short php script to do this.
|
04-19-2006, 06:02 AM
|
Member
GB Beginner
|
|
Join Date: Apr 2006
Posts: 34
|
|
Oops, just spotted a typo, and I can't edit the original post. Please ignore the above script and use :-
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("HTTP/1.1 301 Moved Permanently");
header("Location: $new_loc");
} else {
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$_SERVER['HTTP_HOST']);
}
?>
instead. I missed out the header line in the original.
Also, it is possible to use .htaccess to redirect all files in one location to another, providing the directory structure of the destination remains the same. But the last time I tried that, I got lots of odd problems occuring with other SEO rules in the .htaccess file, so I changed to using the php script instead, which is much easier to maintain IMO.
__________________
Coop
|
04-19-2006, 06:41 AM
|
Member
GB Advanced User
|
|
Join Date: Mar 2006
Posts: 60
|
|
This doesn't work well if there are a couple of thousand pages to redirect, does it?
|
04-19-2006, 05:40 AM
|
Member
GB Beginner
|
|
Join Date: Apr 2006
Posts: 34
|
|
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
|
02-06-2009, 03:45 AM
|
Junior Member
GB Newbie
|
|
Join Date: Feb 2009
Posts: 2
|
|
Smooth 301 redirect php script
This doesn't work well if there are a couple of thousand pages to redirect, does it?
|
02-06-2009, 10:21 AM
|
Administrator
GB Admin GB GEEK
|
|
Join Date: Jun 2006
Location: NJ
Posts: 255
|
|
If you're looking to redirect everything from one domain to another, DNS Made Easy has a domain redirection config that can be setup through their service. It's very simple to use and basically has no limit on the number of pages that can be redirected in an easy way. You can set it up to redirect everything to a specific url or have everything redirect to their exact same file locations but using a different domain name. This is really useful when switching to a different domain as all you need to do is point the new domain to your website, then make the DNS redirection change and presto - your entire site is now running from the new domain.
If you're only wanting to redirect a few thousand pages, but not ALL pages, then do not go the DNS Made Easy route. There are a few ways you can do this.
One way would be to use a single PHP file for each page you want to redirect. In my opinion, that is not the simplest way to do it.
The best way would be if all the files you want to redirect are in the same folder (without any files being in there that you do not want to redirect). If everything is in a single folder or multiple folders, without other non-redirecting files, then you could use either a single error page for those folders on your site which redirects to a single url or htaccess. Use htaccess if you want each page to point to a unique page on another domain, but those pages on the other domain must be named similarly so that you can script it to be mostly automated in the htaccess config file (or httpd.conf).
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|