Ridirecting到一个站点时,有一个?(问号)的网址是什么? [英] Ridirecting to a site when there is a ?(question mark) in the URL?

查看:171
本文介绍了Ridirecting到一个站点时,有一个?(问号)的网址是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个htaccess会被重定向,如果有一个?(问号)的URL,

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

有些样品的URL,

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

问号可能是经过/的任何地方。它应与www的,没有它,以及工作。

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

如果有一个问号,就应该去newsite.com。

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

由于我是新来的htaccess我不知道该如何去做,以及你将新的一些帮助。 谢谢你。

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

推荐答案

您可以试试这个规则

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

如果您使用的是Apache 2.4版本,您可以使用 QSD 标志,而不是重写规则目标URI

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]

说明:

%{QUERY_STRING} 是包含URI(如查询字符串部分特殊的变量: VAR = 123 ),它不包含,因为它已经知道,如果是URI,它的一部分查询字符串%{QUERY_STRING}

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}.

^(。+)$ 是正则表达式它说,在URI查询字符串必须启动 ^ 和结束 $ 包含任何字符一个或更多的时间 + ()捕获组。

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.

因此​​,这基本上意味着如果有查询字符串添加到您的URI的条件的RewriteCond 成为truthy下一行重写规则尽其重定向到新的URL的工作。

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.

这篇关于Ridirecting到一个站点时,有一个?(问号)的网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆