2012-12-28 169 views
3

我试图从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的例子,但没有人为我工作。

谢谢。

回答

9

这很简单,只需使用:

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/
2

就做这样的事情:

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

放置一个?末存在时删除查询。