PHP Redirect
Posted: Sat Oct 17, 2009 10:03 pm
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.
Any help would be appreciated... I think that maybe I need to rethink this...
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();
}