Rewrite rule that removes query string from URL an

2019-08-30 18:35发布

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.

2条回答
成全新的幸福
2楼-- · 2019-08-30 19:09

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.

查看更多
贪生不怕死
3楼-- · 2019-08-30 19:19

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.

查看更多
登录 后发表回答