301 Redirect Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chrispynic99
Forum Newbie
Posts: 9
Joined: Sat Sep 12, 2009 10:06 pm

301 Redirect Question

Post by chrispynic99 »

Hello, I hope to be an active contributor to your site. I have a 301 question about redirects and can't seem to find an answer. We use a bit of code to redirect one of our URL's to a new search engine friendly version.

See here:

http://www.seatkarma.com/search.php?query=Mary+Poppins

Redirects to:

http://www.seatkarma.com/search.php/Mary-Poppins

Here is the code that we are using to do it:

Code: Select all

 
 
if (strlen($_GET['query'])) {
    $query = trim(strtr($_GET['query'], ' ', '-'));
    header('Location: /search.php/'.urlencode($query), true, 301);
    exit();
}
 
 
$query = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], 'search.php/') + 11);
$query = strtr($query, '_', ' ');
$query = strtr($query, '-', ' ');
$query = trim($query);
if (strlen($query) == 0) {
    header('Location: /index.php?message=emptyQuery');
    exit();
}
 
 
Here is the Problem: We used to use an underscore (_) to separate words in our query and as a result we have many URL's in the search engines that are duplicate. For example:

http://www.seatkarma.com/search.php/Mary-Poppins

has a duplicate at

http://www.seatkarma.com/search.php/Mary_Poppins

The search engines don't like duplicate content much, so we want to resolve this by permanently redirecting any requests for http://www.seatkarma.com/search.php/Search_Term

to http://www.seatkarma.com/search.php/Search-Term

Can this be done by modifying the code above somehow? Please advice. Thanks!
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Re: 301 Redirect Question

Post by dude81 »

This can be done by writing a rewrite rule in .htaccess
chrispynic99
Forum Newbie
Posts: 9
Joined: Sat Sep 12, 2009 10:06 pm

Re: 301 Redirect Question

Post by chrispynic99 »

maybe this is an easy fix that I am just overlooking, but we have over 100,000 pages, so this would have to be a wildcard rule.. but how can I set up everything at http://www.seatkarma.com/search.php/Search_Term to go to http://www.seatkarma.com/search.php/Search-Term when "Search Term" could be any one of over 100,000 values??
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: 301 Redirect Question

Post by John Cartwright »

Try replacing this line

Code: Select all

$query = trim(strtr($_GET['query'], ' ', '-'));
to

Code: Select all

$query = trim(str_replace(array(' ', '_'), '-', $_GET['query']));
Basically, str_replace() accepts an array of characters to replace.

You may also want to replace

Code: Select all

if (strlen($_GET['query'])) {
with

Code: Select all

if (!empty($_GET['query'])) {
to avoid notices of using a potentially undefined variable.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: 301 Redirect Question

Post by jackpf »

Sorry to butt in, but shouldn't it be

Code: Select all

$query = trim(str_replace(array(' ', '_'), '-', $_GET['query']));
instead?

Since str_replace() takes the string as its last parameter, whereas strtr() takes it as its first...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: 301 Redirect Question

Post by John Cartwright »

Yes your right, one of PHP many inconsistent functions. I'll edit my original post.
chrispynic99
Forum Newbie
Posts: 9
Joined: Sat Sep 12, 2009 10:06 pm

Re: 301 Redirect Question

Post by chrispynic99 »

Thanks for the help, guys. I had some trouble implementing the code you provided, but I found two ways to fix the problem.

One local fix:

Code: Select all

<?
 
if(strpos($_SERVER['REQUEST_URI'],'_')) {
 
$query = trim(strtr($_SERVER['REQUEST_URI'], '_', '-'));
 
header('Location: '.$query, true, 301);
 
}  
 
if (!empty($_GET['query'])) {
    $query = trim(strtr($_GET['query'], ' ', '-'));
    header('Location: /search.php/'.urlencode($query), true, 301);
    exit();
}
 
 
$query = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], 'search.php/') + 11);
$query = strtr($query, '_', ' ');
$query = strtr($query, '-', ' ');
$query = trim($query);
if (strlen($query) == 0) {
    header('Location: /index.php?message=emptyQuery');
    exit();
}
 
And one fix in the htaccess file:

Code: Select all

 
RewriteRule \.(gif|jpg|css|js|swf|php|png)$ - [L]
RewriteRule !\.(php)$ - [S=4]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ http://www.seatkarma.com/$1-$2-$3 [R=301,L]
RewriteRule ^([^_]*)_(.*)$ http://www.seatkarma.com/$1-$2 [R=301,L]
I'm going with the local fix on this one. Thanks again for your help, guys.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: 301 Redirect Question

Post by John Cartwright »

Code: Select all

if (!empty($_GET['query'])) {
    $query = trim(str_replace(array(' ', '_'), '-', $_GET['query']));
    header('Location: /search.php/'.urlencode($query), true, 301);
    exit();
}
What happens when you tried my solution?

Also remember, it is important you exit() the script after issuing a header('Location: ..) because the script will continue to execute (in most cases you would not want this).
Post Reply