etrader Posted January 10, 2011 Share Posted January 10, 2011 How can I write a 404 file to redirected any 'not found' page with structure of example.com/something/more/final.html to example.com/search?q=something+more+final Thanks Quote Link to comment https://forums.phpfreaks.com/topic/223934-404-redirecting-to-a-search-page/ Share on other sites More sharing options...
cyberRobot Posted January 10, 2011 Share Posted January 10, 2011 You should be able to break apart the URL with explode(): //GET THE CURRENT URL $url = $_SERVER['PHP_SELF']; //BREAK APART THE URL $urlPieces = explode('/', $url); //LOOP THROUGH THE URL PIECES foreach($urlPieces as $currPiece) { echo "<li>$currPiece</li>"; } You'll need to replace the echo statement in the foreach loop with whatever formatting you need to get the search query. Then just redirect the page. Quote Link to comment https://forums.phpfreaks.com/topic/223934-404-redirecting-to-a-search-page/#findComment-1157367 Share on other sites More sharing options...
etrader Posted January 15, 2011 Author Share Posted January 15, 2011 Thanks for your kind assistance. My problem is that $url = $_SERVER['PHP_SELF']; considers the error page at "http://mysite.com/404.php" instead of the misspelled url. For example, if someone write "http://mysite.com/something-wrong.html", $url = $_SERVER['PHP_SELF']; echo $url; will show "http://mysite.com/404.php Quote Link to comment https://forums.phpfreaks.com/topic/223934-404-redirecting-to-a-search-page/#findComment-1159736 Share on other sites More sharing options...
cyberRobot Posted January 18, 2011 Share Posted January 18, 2011 Sorry about that, for the error 404 page we use: getenv("REQUEST_URI") Quote Link to comment https://forums.phpfreaks.com/topic/223934-404-redirecting-to-a-search-page/#findComment-1161339 Share on other sites More sharing options...
etrader Posted January 18, 2011 Author Share Posted January 18, 2011 Thanks a million, it works perfectly Quote Link to comment https://forums.phpfreaks.com/topic/223934-404-redirecting-to-a-search-page/#findComment-1161411 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.