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

后端 未结 1 1147
野的像风
野的像风 2021-01-26 10:33

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
my         


        
相关标签:
1条回答
  • 2021-01-26 10:52

    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.

    0 讨论(0)
提交回复
热议问题