Page 1 of 1

PHP Redirect

Posted: Sat Oct 17, 2009 10:03 pm
by chrispynic99
need to make some changes to a redirect that rewrites the URL on my main search results. The redirect needs to....

1. if fed the url http://example.com/search.php?query=search term, reformat to http://example.com/search.php/search-term

2. if fed the url http://example.com/search.php/search_term (with underscore), reformat with hyphen: http://example.com/search.php/search-term

3. if url search term contains capital letters, set to lower case. example: http://example.com/search.php/Search-Term to http://example.com/search.php/search-term

Here is the code I have so far, which successfully fulfills 1 and 2, but everything I do to accomplish rule 3 fails, causing too many redirects.

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();
}
 
 
Any help would be appreciated... I think that maybe I need to rethink this...

Re: PHP Redirect

Posted: Sat Oct 17, 2009 10:19 pm
by Eric!
I'm not sure why you say you get too many redirects when changing the url to lower case, but try strtolower()

http://php.net/manual/en/function.strtolower.php

Re: PHP Redirect

Posted: Sat Oct 17, 2009 10:20 pm
by requinix
Try this a different way: figure out what the URL is supposed to be, and if it doesn't match what you have then redirect.

Re: PHP Redirect

Posted: Sat Oct 17, 2009 10:26 pm
by chrispynic99
thanks - here is an example of strtolower which causes a failure when I feed it http://example.com/search.php/Search-Term... i'm a confessed hack, so I know this is sloppy

Code: Select all

 
$nquery = strtolower($_SERVER['REQUEST_URI']);
 
if(strpos($nquery,'_')) {
 
    $arr = array("_" => "-", " " => "-");
 
    $query = trim(strtr($nquery, $arr));
 
    $query = strtolower($query);
 
header('Location: '.$query, true, 301);
 
} 
 
if (!empty($_GET['query'])) {
 
    $proc = trim(strtr($_GET['query'], ' ', '-'));
        $query = strtolower($proc);
 
    header('Location: /searca.php/'.urlencode($query), true, 301);
 
    exit();
}
 
 
$query = substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], 'searca.php/') + 11);
$query = strtr($query, '_', ' ');
$query = strtr($query, '-', ' ');
$query = trim(strtolower($query))
;
if (strlen($query) == 0) {
    header('Location: /index.php?message=emptyQuery');
    exit();
}
 
 

Re: PHP Redirect

Posted: Sat Oct 17, 2009 10:32 pm
by chrispynic99
tisairis... sounds like that could work... i'll give it a try - thanks

Re: PHP Redirect

Posted: Tue Oct 20, 2009 10:34 pm
by chrispynic99
Thanks everyone - here is what I ended up doing - proves that sometimes all you need is a quick suggestion to get you going down the right path:

Code: Select all

<?
 
$orig = trim(rtrim($_SERVER['REQUEST_URI'],"-"));
$arr = array("_" => "-", " " => "-", "%20" => "-");
$nquery = strtolower(strtr($orig,$arr));
 
 
if($_SERVER['REQUEST_URI'] == $nquery && empty($_GET['query'])){
 
}
else {
 
if (!empty($_GET['query'])) {
 
    $proc = trim(strtr($_GET['query'], ' ', '-'));
        $query = strtolower($proc);
 
    header('Location: /search.php/'.urlencode($query), true, 301);
 
    exit();
}
else {
 
header('Location: '.$nquery, true, 301);
 
}
}