如何使用 mod_rewrite 删除 URL 的一部分
我试图从这里重定向
www.mysite.com/product.html?item=example
到:
www.mysite.com/example
我的 .htaccess 文件中有这个重写规则,但它不起作用:
RewriteRule ^/product\.html\?item=(.*)$ /$1 [R=301,L]
非常感谢任何帮助。
这是我现在所拥有的:
RewriteCond %{QUERY_STRING} ^item=(.*)$
RewriteRule ^/product\.html$ /%1 [R=301,L]
这是我删除规则开头的正斜杠并在规则末尾添加问号以删除查询字符串后的最终结果:
RewriteCond %{QUERY_STRING} ^item=(.*)$
RewriteRule ^product\.html$ /%1? [R=301,L]
I am trying to redirect from this:
www.mysite.com/product.html?item=example
to:
www.mysite.com/example
I have this rewrite rule in my .htaccess file but it isn't working:
RewriteRule ^/product\.html\?item=(.*)$ /$1 [R=301,L]
Any help is much appreciated.
Here's what I have now:
RewriteCond %{QUERY_STRING} ^item=(.*)$
RewriteRule ^/product\.html$ /%1 [R=301,L]
And here is the Final result after I got rid of forward slash at the beginning of the rule and added a question mark at the end of the rule to get rid of the query string:
RewriteCond %{QUERY_STRING} ^item=(.*)$
RewriteRule ^product\.html$ /%1? [R=301,L]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您位于
.htaccess
文件中,所以您需要从RewriteRule
中删除前导斜杠:在引用正在传递的查询字符串时,Apache 2.2 文档 mod_rewrite 是这样说的:
因此,正如您所发现的,您只需要在替换后添加
?
即可。Because you are in an
.htaccess
file, you'll want to remove the leading slash from yourRewriteRule
:In reference to the query string being passed through, the Apache 2.2 documentation for mod_rewrite says this about it:
So as you discovered, you need only to add a
?
after the substitution.