doulianglou0898 2014-07-26 09:09
浏览 127
已采纳

在网址中有?(问号)时重定向到网站?

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条回答 默认 最新

  • dongpin1850 2014-07-26 09:20
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么