douquan1953 2015-03-06 16:30
浏览 93
已采纳

.htaccess仅在GET参数存在时重定向

I have a client with an old website without 'pretty' URLs. So currently it looks like this:

http://www.domain.com/?w=42&a=5&b=3

The parameter values are numbers only.

Now they want to move the old site to a subdomain and main (www) domain would be home to a new website (WP with SEO friendly URLs).

Now what I would like to do is redirect all requests that come to the /?w=<num> (and ONLY those) to sub.domain.com/?w=<num>, so that existing links (mostly from Google) get redirected to the subdomain page, while the new page works serving new content thorough pretty URLs.

I tried this:

# This works, but redirects the entire www.domain.com
# to sub.domain.com no mather what 
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]

# But this DOESN'T work
RewriteRule   ^/?w(.*)  http://sub.domain.com/?w$1  [R=301,L]

# Also tried to redirect 'by hand', but DIDN'T work either
Redirect 301 /?w=42 http://sub.domain.com/?w=42 

What am I doing wrong? I searched high and low but always end up with this kind of suggestions. Or maybe I'm just searching for wrong keywords ...

Thank you!

  • 写回答

2条回答 默认 最新

  • dszm02606009 2015-03-06 17:27
    关注

    You can't match against the query string inside a rewrite rule or a redirect directive. You need to match against the %{QUERY_STRING} variable. Try:

    RewriteCond %{QUERY_STRING} (^|&)w=[0-9]+(&|$)
    RewriteRule ^(.*)$ http://sub.domain.com/$1 [L,R=301]
    

    Note that the query string gets automatically appended to the end of the rule's destination.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘