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

Vla*_*pov 5 url mod-rewrite

我正在尝试从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
Run Code Online (Sandbox Code Playgroud)

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

我喜欢stackoverflow上的很多例子,但没有人适合我.

谢谢.

小智 13

它很简单,只需使用:

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

如果你要将它添加到wordpress网站,你需要在任何wordpress规则之前添加它:

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Run Code Online (Sandbox Code Playgroud)

但是之后

RewriteBase /
Run Code Online (Sandbox Code Playgroud)


Fel*_*a A 5

只需做这样的事情:

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

?在末尾放置一个以删除存在的查询。