Php redirect not working

Hi,

I have the following code which I am trying to make redirect multiple pages, but it doesn’t seem to work.

Can anyone see anything wrong with it?


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

Thanks!

Shouldn’t the header location be pointing to the new location? In which case, you should be using the $NewPages variable:


header('Location: ' . $NewPages[$Counter]); 

Try putting error_reporting(-1); ini_set(‘display_errors’,1); at the top of the page.

Also, where is $MaxPages being declared?

Thanks, I changed it the $NewPages, but it didn’t work.

The error message says:

Strict Standards: main() [function.main]: It is not safe to rely on the system’s timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Europe/London’ for ‘BST/1.0/DST’ instead in /home/site/public_html/redirect/hash-redirect.php on line 14

Notice: Undefined variable: MaxPages in /home/site/public_html/redirect/hash-redirect.php on line 14

I got the code from here, soI’m not sure what the $MaxPages should be defined as:

Try a Google search for “PHP date[COLOR=#333333]_default_timezone_set()” and no doubt you will find examples of how to use the function.

You should hard code the $MaxPages and set the value to 2.

Since all the values are known it would be easier to use a for(…). loop.

Tapped laboriously from a tablet :frowning:

[/COLOR]

You can fix the strict standards error by invoking the date_default_timezone_set() function at the top of your script (with your timezone passed as an argument - see the PHP.net manual for the supported timezones).

The $MaxPages variable looks like it should be the count of one of the URI arrays (it doesn’t matter which one, since they both should contain the same amount of elements):


$MaxPages = count($OldPages);

EDIT: John_Betong beat me to it this time :wink:

Thanks, I now have the following, but still it doesn’t redirect :frowning:


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


I also tried


$MaxPages = "2";  

I am unable to check but curious to know the value of count($OldPages);

Does PHP arrays have an initial and hidden value of zero which would make the count equal to 3?

Try $MaxPages without the quotes. When enclosed in quotes the value is a string and not strong :slight_smile:

I tried setting it to 3, but got this:
Notice: Undefined index: 3

Try 2 instead of 3.

I tried 2 and there are no errors, but the redirect doesn’t happen.

Nope, the count() function in PHP, when applied to arrays, will count the number of present elements in the array (regardless of index), so:


$a[4] = 'a';
$a[10] = 'b';
echo count($a); // 2

Try this:

http://www.johns-jokes.com/downloads/sp-c/redirect-ebsoloutions/

If there is anything you do not understand then please feel free to ask questions.

Thanks, I tried page 1 and it out putted the URI and also the code. Page 2 seems to have errors on :confused:

It has just occurred to me that the value of $_SERVER[‘REQUEST_URI’] will contain a preceding forward slash. You will therefore want to use substr() (or trim(), whichever takes your fancy) against that value to remove the preceding forward slash. There’s also a more optimal way of checking the validity of the URI for your scenario - by using the in_array() function. Here’s how I’d rewrite your script:


date_default_timezone_set("UTC");

// redirects
$redirect = array(
  'page1.shtml' => 'newpage1.shtml',
  'page2.shtml' => 'newpage2.shtml'
);

$oldPage = substr($_SERVER['REQUEST_URI'], 1);

if(in_array($oldPage, array_keys($redirects), true)) {
  header('HTTP/1.1 301 Moved Permanently');
  header("Location: {$redirects[$oldPage]}");
  die;
}

// no page found, so send out a 404 status code

Thank you :slight_smile:

I updated both page1.php and page2.php, removed the errors and an explanation as to why the errors would occur when calling page2.php.

http://www.johns-jokes.com/downloads…-ebsoloutions/

Many thanks :smiley: it works on your version, but not on my site :confused: I’ll have a play around with it.

Can I use absolute URLs?

Many thanks!

I am glad it worked, so much easier than posting messages backwards and forwards.

If it is not working on your site, activate the DEBUG comments (change the 0 to 1) to display the variables. You should be able to find why the validation is not calling header(‘Location: …’);

>>> Can I use absolute URLs?
Also remove the remarks on the following and select a more suitable $_SERVER parameter:



// echo '<pre>'; print_r($_SERVER); echo '</pre>';