重定向301动态网址htaccess

时间:2015-01-28 13:41:44

标签: .htaccess mod-rewrite redirect

我的htaccess规则如下

RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /detail_new?
location=$1&id=$2&name=$3 [L,QSA]

当我输入网址为http://example.com/location/id/name

时,它工作正常

但是当我输入http://example.com/detail_new.php?location=Panchkula&id=123&name=ABC时,页面也会打开。

但我希望http://example.com/detail_new.php?location=XYZ&id=123&name=ABC始终可以重定向到http://example.com/location/id/name

我在PHP中使用如下所示

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
 if ((false !== strpos($url,'detail_new')) {
   $rebuilt_url = "http://example.com/location/id/name";
   header ("HTTP/1.1 301 Moved Permanently");
   header("Location: {$rebuilt_url}") ;  // redirect to this url
}

我怎么能在htaccess中做同样的事。

1 个答案:

答案 0 :(得分:1)

您可以添加此规则,将实际网址重定向到您之前的规则之前的

RewriteCond %{THE_REQUEST} \s/+detail_new\?location=([^\s&]+)&id=([^\s&]+)&name=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=302,L,NE]

RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /detail_new?location=$1&id=$2&name=$3 [L,QSA]