重定向 301 动态 URL htaccess

Redirect 301 Dynamic URLs htaccess

我的 htaccess 中有如下规则

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

当我输入 URL 作为 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 中做同样的事情。

您可以添加此规则以将实际的 URL 重定向到漂亮的 之前的规则 :

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=&id=&name= [L,QSA]