remove query string from end of URL using .htaccess

痴心易碎 提交于 2019-12-17 16:37:51

问题


I'm trying to remove string from the end of URL:

For example i have a structure like this:

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

I would like to remove ?post_type=question from the end of URL.

I found a lot of examples on stackoverflow, but no one works for me.

Thank you.


回答1:


it's simple, just use:

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

If you are going add this to wordpress website, you need add it before any wordpress rules:

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

but after

RewriteBase /



回答2:


Just do something like this:

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

Place a ? at the end to remove the query when present.



来源:https://stackoverflow.com/questions/14071671/remove-query-string-from-end-of-url-using-htaccess

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!