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 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答