Page 1 of 1

301 Redirect Question

Posted: Sat Sep 12, 2009 10:26 pm
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!

Re: 301 Redirect Question

Posted: Sat Sep 12, 2009 11:05 pm
by dude81
This can be done by writing a rewrite rule in .htaccess

Re: 301 Redirect Question

Posted: Sun Sep 13, 2009 7:54 am
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??

Re: 301 Redirect Question

Posted: Sun Sep 13, 2009 12:42 pm
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.

Re: 301 Redirect Question

Posted: Sun Sep 13, 2009 12:46 pm
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...

Re: 301 Redirect Question

Posted: Sun Sep 13, 2009 12:49 pm
by John Cartwright
Yes your right, one of PHP many inconsistent functions. I'll edit my original post.

Re: 301 Redirect Question

Posted: Tue Sep 15, 2009 6:56 pm
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.

Re: 301 Redirect Question

Posted: Tue Sep 15, 2009 8:36 pm
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).