Jump to content

multiple redirects in one file?


toolman

Recommended Posts

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!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by cyberRobot
Link to comment
Share on other sites

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'];

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 18

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 19

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.