Ridirecting to a site when there is a ?(question mark) in the URL?

那年仲夏 提交于 2020-01-22 03:34:08

问题


I need an htaccess which redirects if there is a ?(question mark) in the URL,

Some sample URL's,

mysite.com/?p=44
mysite.com/?m=44
mysite.com/?cat=44
mysite.com/index.php?cat=44

The question mark could be anywhere after the "/". It should work for with the "www" and without it as well.

If there is a question mark, it should go to newsite.com.

Since I'm new to htaccess I don't know how to go about it and new some help from you'll. Thanks.


回答1:


You can try this rule

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^ http://newsite.com/? [L,R=301]

If you are using Apache v2.4 you can use QSD flag instead of ? in your RewriteRule target URI

RewriteRule ^ http://newsite.com [L,R=301,QSD]

Description:

The %{QUERY_STRING} is special variable which contains query string part of the URI (e.g: var=123) And it doesn't contain ? because it already knows if ? is in the URI, it is part of query string %{QUERY_STRING}.

The ^(.+)$ is regex which says the query string in the URI must start ^ and end $ with containing any character . one or more time + within the () capturing group.

So this basically means if any query string appended to your URI the condition RewriteCond becomes truthy and the next line RewriteRule do its work to redirect to your new URL.



来源:https://stackoverflow.com/questions/24969396/ridirecting-to-a-site-when-there-is-a-question-mark-in-the-url

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