toolman Posted September 3, 2014 Share Posted September 3, 2014 Hi, Is it possible to create a PHP file that 301 redirects specific pages if the file is included on those pages? I am wanting to create a file similar to a htaccess file and then include that file on the pages I want to redirect. For example "if page is page1.shtml redirect to page2.shtml" "if page is page5.shtml redirect to page10.shtml" etc and on page1.shtml and page5.shtml, I will include the above PHP include file Hope that makes sense Thanks! Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 3, 2014 Share Posted September 3, 2014 When someone visits one of these broken links, are they brought to an 404 error page that you control? If so, you could modify the page to see which page the visitor attempted to view with: getenv('REQUEST_URI') From there you can execute a switch statement, if construct, database query, etc. to determine where to redirect the visitor. Quote Link to comment Share on other sites More sharing options...
toolman Posted September 3, 2014 Author Share Posted September 3, 2014 Hi, No they are just old pages. I have this, but it doesn't seem to work. // Old Locations $OldPages[1] = 'page1.shtml'; $OldPages[2] = 'page2.shtml'; // New Locations Below! $NewPages[1] = 'newpage1.shtml'; $NewPages[2] = 'newpage2.shtml'; $Counter = 1; while ($Counter <= $MaxPages) { if ($_SERVER['REQUEST_URI'] === $OldPages["$Counter"]) { header("HTTP/1.1 301 Moved Permanently"); header('Location: ' . $OldPages["$Counter"]); exit(); } ++$Counter; } Can you see anything wrong with it? Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 3, 2014 Share Posted September 3, 2014 (edited) Could you provide a little more information on your current setup? For all the broken links (old pages), do you have some sort of placeholder script which imports the redirect code pasted above? Also, are you unable to use an .htaccess file or are you just trying to avoid .htaccess for some reason? As for your script, is $MaxPages defined somewhere? Edited September 3, 2014 by cyberRobot Quote Link to comment Share on other sites More sharing options...
toolman Posted September 3, 2014 Author Share Posted September 3, 2014 I am including the above code into each .shtml page that I want to redirect. It's in an external file. I am trying to redirect URLs with a # in and this doesn't seem to work in my htaccess file. I found the code here, so not sure what the $MaxPages should be defined as: http://brugbart.com/php-redirect Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 3, 2014 Share Posted September 3, 2014 I found the code here, so not sure what the $MaxPages should be defined as: $MaxPages would be a count of the entries in $OldPages. It used to loop through the entire array. However, you could use array_search() instead. More information can be found here: http://php.net/manual/en/function.array-search.php Basically, you could try something like this: $key = array_search($_SERVER['REQUEST_URI'], $OldPages); if($key !== false) { header("HTTP/1.1 301 Moved Permanently"); header('Location: ' . $NewPages[$key]); exit; } Note that I changed the second call to header() so that it redirects to the value in the $NewPages array. Otherwise you would be redirecting to the old page and end up in an infinite loop. Also, you may need to double check the value contained within $_SERVER['REQUEST_URI']. Make sure it matches the values used in the $OldPages array. To check the value, you can echo it: echo $_SERVER['REQUEST_URI']; Quote Link to comment Share on other sites More sharing options...
toolman Posted September 3, 2014 Author Share Posted September 3, 2014 Thanks for the help. I now have: // Old Locations $OldPages[1] = 'page1.shtml'; $OldPages[2] = 'page2.shtml'; // New Locations Below! $NewPages[1] = 'newpage1.shtml'; $NewPages[2] = 'newpage2.shtml'; $MaxPages = count($OldPages); $Counter = 1; while ($Counter <= $MaxPages) { $key = array_search($_SERVER['REQUEST_URI'], $OldPages); if($key !== false) { header("HTTP/1.1 301 Moved Permanently"); header('Location: ' . $NewPages[$key]); exit; } ++$Counter; } but still no redirect. I checked the $_SERVER['REQUEST_URI'] value and its the same as I'm using. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 3, 2014 Share Posted September 3, 2014 With array_search(), you don't need the loop. <?php // Old Locations $OldPages[1] = 'page1.shtml'; $OldPages[2] = 'page2.shtml'; // New Locations Below! $NewPages[1] = 'newpage1.shtml'; $NewPages[2] = 'newpage2.shtml'; $key = array_search($_SERVER['REQUEST_URI'], $OldPages); if($key !== false) { header("HTTP/1.1 301 Moved Permanently"); header('Location: ' . $NewPages[$key]); exit; } ?> Of course, this may not work since the values in the $OldPages array are not root-relative values. Did you try echoing $_SERVER['REQUEST_URI'] to see if it matches a value in your array? Quote Link to comment Share on other sites More sharing options...
toolman Posted September 3, 2014 Author Share Posted September 3, 2014 Thanks, I echoed $_SERVER['REQUEST_URI'] and got the correct path, but using the above code, I get this error: Warning: Cannot modify header information - headers already sent by (output started at /home/site/public_html/redirect/hash-redirect.php:2) in /home/site/public_html/redirect/hash-redirect.php on line 18Warning: Cannot modify header information - headers already sent by (output started at /home/site/public_html/redirect/hash-redirect.php:2) in /home/site/public_html/redirect/hash-redirect.php on line 19 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 3, 2014 Share Posted September 3, 2014 (edited) The header() function needs to be called before anything has been outputted to the screen. Do you still have the echo before the $_SERVER variable? If so, removing it should make the errors go away. Edited September 3, 2014 by cyberRobot Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 3, 2014 Share Posted September 3, 2014 I am trying to redirect URLs with a # in and this doesn't seem to work in my htaccess file. For what it's worth, the following thread talks about redirecting URLs with a #: http://stackoverflow.com/questions/8910170/htaccess-redirect-does-this-work-for-all-anchor-links-in-page If that doesn't work, perhaps someone with more knowledge of .htaccess files can help. Quote Link to comment Share on other sites More sharing options...
toolman Posted September 3, 2014 Author Share Posted September 3, 2014 I've placed it right at the top of the document using this: <!--#include virtual="/redirect/hash-redirect.php" --> Then this is my current code in that file: $_SERVER['REQUEST_URI']; error_reporting(-1); ini_set('display_errors',1); date_default_timezone_set("UTC"); // Old Locations $OldPages[1] = 'page1.shtml'; $OldPages[2] = 'page2.shtml'; // New Locations Below! $NewPages[1] = 'newpage1.shtml'; $NewPages[2] = 'newpage2.shtml'; $MaxPages = count($OldPages); $Counter = 1; while ($Counter <= $MaxPages) { if ($_SERVER['REQUEST_URI'] === $OldPages["$Counter"]) { header("HTTP/1.1 301 Moved Permanently"); header('Location: ' . $NewPages["$Counter"]); exit(); } ++$Counter; } Quote Link to comment 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.