【问题标题】:Multiple htaccess redirections with regex使用正则表达式进行多个 htaccess 重定向
【发布时间】:2017-01-19 17:12:46
【问题描述】:

我需要创建重定向:

www.mysite.com/out/cars/ 到 https://cars.example.com/?aid=123(不工作)

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

www.mysite.com/out/cars/volvo/ 到https://cars.example.com/volvo/?aid=123(有效)

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/ 到https://cars.example.com/volvo/vagon/?aid=123(有效)

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

www.mysite.com/out/bikes/ 到 https://bikes.example.com/?aid=123(不工作)

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

www.mysite.com/out/bikes/suzuki/ 到 https://bikes.example.com/suzuki/?aid=123(不工作)

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 到https://bikes.example.com/suzuki/volusia/?aid=123(有效)

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/ 到https://atv.example.com/bmw/black/big/best/?aid=123(有效)

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

问题是,有些规则有效,但不是全部。我猜他们会以某种方式干扰,但我试图改变我能做的,但没有 100% 有效:(

例如会发生这样的事情:

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

重定向到:

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

等等。 :(

有人可以帮忙吗?非常感谢您!

【问题讨论】:

    标签: php regex .htaccess redirect


    【解决方案1】:

    也许一个更简单的正则表达式就足够了? (从示例中推断出明显的规则,因为它们没有明确说明)

    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

    【讨论】:

    • 谢谢,不幸的是它只是将我重定向到 http://.example.com/?aid=123 。好像它没有使用捕获组:(不知道为什么...
    • 当我将 $1 和 $2 更改为 %1 和 %2 时效果更好,但并不完全。 www.mysite.com/out/bikes/suzuki/volusia/ 重定向到 bikes.example.com/suzuki/?aid=123。它错过了第二个捕获组的一部分。你不知道为什么?
    • 我撤消了那个编辑(我不确定字符串是如何开始的,老实说)。但是我不知道为什么/如何在第二个问题中模式无法匹配字符串的其余部分(适用于 regex101)
    • 我也不太明白 :( 不确定服务器设置是否有问题?
    • 我不得不将您的代码更改为 RewriteCond %{REQUEST_URI} /out/([^/]+)/?(.*) [NC]RewriteRule ^ http://%1.example.com/%2?aid=123 [NE,L,R=302] 。区别在于 %{REQUEST_URI}%1%2($1 用于 RewriteRule)。现在它就像一个魅力!谢谢:)
    【解决方案2】:

    尝试:

    RewriteRule ^out/cars/?$ https://cars.othersite.com/?aid=123 [NC,NE,L,R=302]
    RewriteRule ^out/cars/([^/]+)/?$ https://cars.othersite.com/$1/?aid=123 [NC,NE,L,R=302]
    RewriteRule ^out/cars/([^/]+)/([^/]+)/?$ https://cars.othersite.com/$1/$2/?aid=123 [NC,NE,L,R=302]
    RewriteRule ^out/bikes/?$ https://bikes.othersite.com/?aid=123 [NC,NE,L,R=302]
    RewriteRule ^out/bikes/([^/]+)/?$ https://bikes.othersite.com/$1/?aid=123 [NC,NE,L,R=302]
    RewriteRule ^out/bikes/([^/]+)/([^/]+)/?$ https://bikes.othersite.com/$1/$2/?aid=123 [NC,NE,L,R=302]
    RewriteRule ^out/atv/([^/]+)/([^/]+)/([^/]+)/?$ http://atv.example.com/$1/$2/$3/?aid=123 [NC,NE,L,R=302]
    

    【讨论】:

    • 我不知道为什么,但是这一行重定向不起作用:(
    • 你添加了其他行吗?
    猜你喜欢
    • 1970-01-01
    • 2019-08-20
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 2014-12-05
    相关资源
    最近更新 更多