RewriteCond%{QUERY_STRING}到特定的URL

时间:2015-03-18 15:45:50

标签: .htaccess redirect

我将旧网站迁移到WordPress,目前正在为.aspx页面设置各种301重定向。旧站点使用查询字符串来显示某些页面,因此我想将这些页面映射到新的SEO URL。

我目前的基本'静态'页面工作正常:

Redirect 301 /about-us.aspx http:// example.com/about-us/
Redirect 301 /get-in-touch.aspx http:// example.com/contact/
Redirect 301 /services.aspx http:// example.com/services/

问题在于以下查询字符串:

Redirect 301 /services.aspx?type=14 http:// example.com/services/service-a/
Redirect 301 /services.aspx?type=17 http:// example.com/services/service-b/

我已经读过,我必须使用RewriteCond才能正确地重定向这些查询字符串。我在simonecarletti的博客上看到,以下内容可以将查询字符串传递到URI的末尾。

RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/page\.php$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ http://mydomain.site/page/%1.pdf [R=302,L]

这对我来说不实用,因为我在以服务命名的URL中有关键字。身份证号码毫无意义。

在这里阅读了几个问题[11544773,25621398,17919953]之后,我一直在尝试各种建议而没有任何运气。

所有这一切都发生在我访问的地方:

/services.aspx?type=14

我被重定向到:

/services/?type=14

如何重定向到新URI,例如

/services/service-a/

以下是我正在玩的一堆代码:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^type=14$
#RewriteCond %{REQUEST_URI}  ^/services\.aspx$
RewriteRule ^ http://example.com/service-a/? [R=301,L]

1 个答案:

答案 0 :(得分:1)

您拥有的代码应该可以使用,您只需要检查URI中的服务:

RewriteCond %{QUERY_STRING} ^type=14$
RewriteRule ^services\.aspx$ http://example.com/services/service-a/? [R=301,L]
相关问题