简体   繁体   中英

Multiple htaccess redirections with regex

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!

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

Try with:

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM