简体   繁体   中英

.htaccess rewrite rule - matching and forwarding image requests with certain character in the name

I currently have the following .htaccess file in my ../test/media folder to redirect all jpgs to another server

RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule /([^./]+\.(jpg))$ http://www.example1.com/img/gallery/$1

This works great thanks to help on this site.

BUT, i have a caveat. if the image being requested has a filename like *_p.jpg then I would like it directed to another location on the target server but without the '_p' part.

eg

http://blah/media/catalog/product/3/8/3848038_5856.jpg gets redirected to http://www.example1.com/img/gallery/3848038_5856.jpg currently and that works.

However if i request http://blah/media/catalog/product/3/8/3848038-3565_p.jpg then I want it to redirect to http://www.example1.com/img/norm/high/848038-3565.jpg rather than http://www.example1.com/img/gallery/848038-3565.jpg

Is this even possible??

Thanks in advance

This should work for you

RewriteEngine on

# First we redirect *_p.jpg files
RewriteCond %{HTTPS} !on
RewriteRule /([^./]+)_p\.jpg$ http://www.example1.com/img/norm/high/$1.jpg

# ... and then other jpgs
RewriteCond %{HTTPS} !on
RewriteRule /([^./]+\.jpg)$ http://www.example1.com/img/gallery/$1

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