繁体   English   中英

根据GET参数重定向到新域

[英]Redirect to a new domain based on GET parameters

我已经多次使用301重定向,但这次我遇到了一个新问题。 我在Joomla上有一个旧网站,网站上的所有页面都由相同的index.php文件加载。 示例:

URL of a page           : http://domain.com/index.php?option=com_content&view=section&id=4&Itemid=54
URL of a different page : http://domain.com/index.php?option=com_content&view=section&id=1&Itemid=55

如何将每个页面重定向到其他页面?

例如,我想将页面http://domain.com/index.php?option=com_content&view=section&id=4&Itemid=54重定向到http://newdomain.com/location/

我需要根据GET参数进行301重定向,但我无法弄清楚如何。

非常感谢您的帮助!

如果可以在htaccess文件中执行此操作?

使用简单的开关

switch($_GET['id']){
    case 1:   $myPage = "http://newdomain.com/location/"; break;
    case 4:   $myPage = "http://othernewdomain.com/location/"; break;
    default:  $myPage = "http://google.com"; break;
}
header("Location: {$myPage}");

当然你可以轻松添加更多参数并切换它们......就像

if($_GET['id'] == 1 && $_GET['item'] == 55 ){
    $goto = 1;
}
switch($goto){ ... }

htaccess会有些像

RewriteEngine On
Redirect 301 /index.php?option=com_content&view=section&id=4&Itemid=54 http://www.domain.com/

通过GET发送的参数将作为以下结构接收:

array(n):
    name_1 = 'value1',
    name_2 = 'value2',
    ...
    name_n = 'valueN',

您可以通过$_GET变量访问它们,其中每个键都等于参数的名称,例如,要访问绑定到名称为'foo'的参数的值,您可以:

$bar = $_GET['foo'];

在你的PHP脚本上。 因此,您只需创建一个简单的流控制器或一个有效检测通过GET发送的参数并重定向到您正在寻找的URL的开关

final class Redirect {

    public static function toUrl($url, $parameters = array(), $statusCode = 303) {
        header('Location: ' . $url . http_build_query($parameters), true, $statusCode);
    }

}

$parameter = $_GET['key'];

switch ($parameter) {
    case '<the specific parameter>':
        Redirect::toUrl('http://example.com/');
        break;
    case '<other case>':
        ...
}

$parameters参数是可选的,您可以使用它通过GET将一些其他数据发送到新URL,您还可以控制通过可选的$statusCode参数发送的状态代码。

你可以使用这样的规则你的DOCUMENT_ROOT/.htaccess文件:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^option=com_content&view=section&id=4&Itemid=54$
RewriteRule ^(index\.php)?$ http://newdomain.com/location/? [L,NC,R=302]

确保将这些规则放在任何其他规则之前。

之后的字符串? 在URL中称为查询字符串。 在PHP中,它包含在QUERY_STRING环境变量中。 因此,您可以使用index.php脚本中的if语句来执行您想要执行的操作,以测试查询字符串的内容,如下所示:

if($_SERVER['QUERY_STRING']=="option=com_content&view=section&id=4&Itemid=54") { header("Location: http://newdomain.com/location/"); }

使用htaccess我相信你可以做到这一点。

这应该工作:

  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
  RewriteCond %{HTTP_HOST} ^www.olddomain.com$
  RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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