使用查询字符串停止.htaccess重定向

问题描述:

我的站点使用.htacess文件将根域名重定向到另一个URL.当前的重定向如下所示:

My site uses an .htacess file to redirect the root domain name to another URL. The current redirect looks like this:

RewriteEngine onRewriteRule ^ $ http://example2.com/[R = 301,L]

我只希望将根域重定向(将example1.com重定向到example2.com),因此上述内容可以正常工作.但是,如果在旧的URL中使用了查询字符串.例如

I only want the root domain to be redirected (example1.com to example2.com) so the above works correctly. However if a query string is used within the old URL. For example

http://example1.com/?pages=3

我仍然不需要重定向.我只是在使用查询字符串时遇到重定向问题,其他都没问题:

The redirect still works which I don't want. Im just having the redirect problem when a query string is used, anything else is fine:

http://example1.com/-是否重定向✓

http://example1.com/home -不重定向✓

http://example1.com/?page=3 -重定向X是否需要-不重定向

http://example1.com/?page=3 - Does Redirect X - need this not to redirect

任何人都可以帮忙吗?

您可以放一个?在目标末尾丢弃旧的查询字符串:

You can put a ? at the end of the destination to discard old querystring :

RewriteEngine on
RewriteRule ^$ http://example2.com/? [R=301,L]

如果您不希望在请求包含查询字符串时进行重定向,则可以使用:

If you dont want to redirect when the request contains query string, you may use :

RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^$ http://example2.com/ [R=301,L]