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条)

报告相同问题?

悬赏问题

  • ¥50 gki vendor hook
  • ¥15 centos7中sudo命令无法使用
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联