使用.htaccess从URL末尾删除查询字符串

7

我希望你能够帮助我删除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

我希望你能帮我将URL末尾的?post_type=question删除。
我在stackoverflow上找到了很多例子,但是没有一个适用于我。
谢谢。

了解如何使用htaccess删除URL查询字符串的特定部分 https://helponnet.com/2021/06/07/remove-specific-query-string-with-htaccess/ - Amit Verma
2个回答

21

很简单,只需使用:

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 /

5

只需像这样做:

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

在末尾加上一个问号?,可以在查询存在时删除查询。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接