Rewrite rule that removes query string from URL and redirect

耗尽温柔 提交于 2019-12-11 17:48:52

问题


Someone is creating erroneous links by adding a question mark and random characters to random pages on my website in the following format:

  • www.mydomain.com/?57237933 (only numbers)
  • www.mydomain.com/folder1/?709d0c24941 (with letters)
  • www.mydomain.com/folder1/page-name/?20?9h572 (double question mark)

I have found a block rule for robots.txt but need a stronger htaccess solution. I am looking for a htaccess rewrite rule that can remove the ? and everything following it from any URLs, redirecting the visitor to the original URL.

I've found solutions to remove, for example, ?id=123 but when id= isn't there I haven't found a solution.


回答1:


The following two rules check for the presence of query string and redirect to the no-query string version of the page:

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

Since you are worried about search engines crawling wrong version of page, consider changing to R=301 once you've tested. Also, you might want to consider returning a 404 error to discourage the use of query strings and tell the search engines that there is no such thing as http://mydomain.com/page/?1234.




回答2:


I cannot test it right now, but I think this would be the solution to your question.

RewriteRule   ^([^?]+)\?.*$ $1 [L]

Please verify it and tell me if it was ok.



来源:https://stackoverflow.com/questions/10474260/rewrite-rule-that-removes-query-string-from-url-and-redirect

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