使用的.htaccess删除URL的末尾查询字符串(remove query string from

2019-07-17 16:01发布

我试图从URL的末尾删除字符串:

例如我有这样的结构:

http://sitename.com/category/first-category-name/?post_type=question
http://sitename.com/category/second-category-name/?post_type=question
http://sitename.com/category/...-category-name/?post_type=question

我想删除?post_type=question从URL的末尾。

我发现了很多关于计算器的例子,但没有人对我的作品。

谢谢。

Answer 1:

这很简单,只需使用:

RewriteCond %{QUERY_STRING}    "post_type=" [NC]
RewriteRule (.*)  /$1? [R=301,L]

如果你打算将它添加到WordPress的网站,你需要之前的任何WordPress的规则添加:

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

但是之后

RewriteBase /


Answer 2:

只是这样做:

RewriteEngine On
RewriteBase /
# Make sure there is a query string
RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*  /? [R=301,L]

放置? 在结束时当存在时除去该查询。



文章来源: remove query string from end of URL using .htaccess