Forum Moderators: coopster

Message Too Old, No Replies

How to search a simple keyword string and 301 redirect if X is presen

Need to redirect any page like search.php?q=/

         

rollinj

10:41 pm on Jun 23, 2007 (gmt 0)

10+ Year Member



I have a tag folder that has the search.php?q= portion mod_rewritten away... so every tag is actually a search but looks like domain.com/tag/searched_tag_here

My problem now.. is that after 301 redirecting various other domains that I've owned over the years: I've ended up with pages indexed in google that no longer look like tags but the actual folders they were redirected from (ie. domain.com/tag/folder1/folder2/index.html )

My solution - is that since tag/folder1/folder2/index.html is the same as search.php?q=folder1/folder2/index.html - I just need to 301 redirect the search page to the homepage when a slash is found in the Query field correct?

How do I do it!? Thanks!

eelixduppy

1:26 am on Jun 24, 2007 (gmt 0)



Maybe something like this:

if(strpos($_GET['q'],'/')!== FALSE) {
[url=http://www.php.net/header]header[/url]('Location: other_page.html');
exit();
}

rollinj

4:58 pm on Jun 24, 2007 (gmt 0)

10+ Year Member



Ok that looks like what I need thanks! So basically.. for my better understanding.. strpos searches the get query for a slash and if none is found.. it returns boolean false and redirects to said page?

Thank you!

eelixduppy

9:03 pm on Jun 24, 2007 (gmt 0)



>> strpos searches the get query for a slash

Yes; if it finds a match it returns the position in the string (an integer value, including zero), however, if nothing is found, it returns FALSE. The condition is checking to see if something is found, or it is NOT (!==) FALSE, and if it's true, redirect the page.

Hope that explains it :)

rollinj

11:02 pm on Jun 24, 2007 (gmt 0)

10+ Year Member



It worked perfectly! Thank you so much! Actually I changed the php redirect code to:

if(strpos($_GET['q'],'/')!== FALSE) {
Header("HTTP/1.1 301 Moved Permanently");Header("Location: [domain.com...]
exit();
}

as I noticed it occasionally redirected with a 302 using just header:location domain as provided with the original code - the Header("HTTP/1.1 301 Moved Permanently"); fixed that!

Thanks so much again - I've been worrying about that problem for weeks!