dqrdlqpo775594 2017-01-19 17:12
浏览 17
已采纳

使用正则表达式进行多次htaccess重定向

I need to create redirects:

www.mysite.com/out/cars/ to https://cars.example.com/?aid=123 (not working)

RewriteCond %{THE_REQUEST} /out/cars [NC]
RewriteRule ^ https://cars.othersite.com/?aid=123 [NE,L,R=302]

www.mysite.com/out/cars/volvo/ to https://cars.example.com/volvo/?aid=123 (works)

RewriteCond %{THE_REQUEST} /out/cars/([^/]+) [NC]
RewriteRule ^ https://cars.othersite.com/%1/?aid=123 [NE,L,R=302]

www.mysite.com/out/cars/volvo/vagon/ to https://cars.example.com/volvo/vagon/?aid=123 (works)

RewriteCond %{THE_REQUEST} /out/cars/([^/]+)/([^/]+) [NC]
RewriteRule ^ https://cars.othersite.com/%1/%2/?aid=123 [NE,L,R=302]

www.mysite.com/out/bikes/ to https://bikes.example.com/?aid=123 (not working)

RewriteCond %{THE_REQUEST} /out/bikes [NC]
RewriteRule ^ https://bikes.othersite.com/?aid=123 [NE,L,R=302]

www.mysite.com/out/bikes/suzuki/ to https://bikes.example.com/suzuki/?aid=123 (not working)

RewriteCond %{THE_REQUEST} /out/bikes/([^/]+) [NC]
RewriteRule ^ https://bikes.othersite.com/%1/?aid=123 [NE,L,R=302]

www.mysite.com/out/bikes/suzuki/volusia to https://bikes.example.com/suzuki/volusia/?aid=123 (works)

RewriteCond %{THE_REQUEST} /out/bikes/([^/]+)/([^/]+) [NC]
RewriteRule ^ https://bikes.othersite.com/%1/%2/?aid=123 [NE,L,R=302]

www.mysite.com/out/atv/bmw/black/big/best/ to https://atv.example.com/bmw/black/big/best/?aid=123 (works)

RewriteCond %{THE_REQUEST} /out/atv/([^/]+)/([^/]+)/([^/]+)/ [NC]
RewriteRule ^ http://atv.example.com/%1/%2/%3/?aid=123 [NE,L,R=302]

Thing is, some rules work but not all of them. I guess they interfere in some way, but I tried to change what I could and nothing worked for 100% :(

What happens is for example that this:

www.mysite.com/out/bikes/suzuki/

redirects to:

https://bikes.example.com/suzuki/%20http/?aid=123

etc. :(

Anybody could help? Thank you very much in advance!

  • 写回答

2条回答 默认 最新

  • doulang6695 2017-01-19 17:39
    关注

    maybe a simpler regex would still suffice? (inferring the apparent rules from the examples, since they are not explicitly stated)

    RewriteCond %{THE_REQUEST} .*?/out/(cars|bikes|atv)/?(.*) [NC]
    RewriteRule ^ http://$1.example.com/$2?aid=123 [NE,L,R=302]
    

    https://regex101.com/r/vp9jrA/2

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

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作