简体   繁体   中英

PHP 301 redirect multiple URLs

I am trying to move multiple dynamic URLs using a php 301 redirect, not htaccess.

I can't work out how to put it together though as I'm a little out of my depth, this is all have so far.

<?php 
header("HTTP/1.1 301 Moved Permanently"); 
header("Location: http://www.new-website/new-folder/ $url="http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$url=str_replace('http://www.website.com/old-folder/','',$url);
echo $url; "); 
?>

Old URLs: http://www.website.com/old-folder/news-article/full.php?= anything

New URLs: http://www.new-website.com/new-folder/news-article/full.php?= anything

I want to insert anything that comes after the second / of the current URL into the Header Location

Anything after /old-folder/ will be different each time.

In simple terms I am trying to do this:

<?php 
header("HTTP/1.1 301 Moved Permanently"); 
header("Location: http://www.new-website/new-folder/ *insert end of URL from current address bar here* "); 
?>

You are setting a variable inside the header function, then trying to call functions inside there too. The header (with location) should just be given a location to redirect too.

The second header call will replace the first one, so you can only really use one or the other:

$url = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$url = str_replace('http://www.website.com/old-folder/','',$url);

header("Location: http://www.new-website/new-folder/$url"); 

OR

header("Status: 301 Moved Permanently");
$url = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$url = str_replace('http://www.website.com/old-folder/','',$url);

Hopefully you now have something like: news-article/full.php?=anything

You can now do:

header("HTTP/1.1 301 Moved Permanently"); 
header("Location: http://www.new-website/new-folder/".$url); 

But, as suggested already, its best to approach this with Htaccess instead of php.

Best way you try with php. Just create a one temp variable. Write you own JavaScript redirection using windows.location.href. Assign those script to that temp variable and echo it.

Let's try that will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM