htaccess redirect all images to different location

2019-07-24 04:27发布

This is the rewrite rule that I am working with:

RewriteRule (.*\.(jpe?g|gif|bmp|png))$ http://www.newurl.com/?image=$1

What I want is all images to get redirected to the new location and this above rule works paritally. The problem is that $1 will contain the whole url from the old request and not just the filename. I only need the filename and everything that I try doesnt seem to work properly. Any help is appreciated.

I think i've figured it out. If anyone has any other solutions though, please don't hesitate to add them. Thanks.

RewriteRule ^([^.]+)/([^.]+\.(jpe?g|gif|bmp|png))$ http://www.newurl.com/$2

Thanks.

1条回答
男人必须洒脱
2楼-- · 2019-07-24 05:09

Actually this rule will be better for you:

RewriteRule ([^.]+\.(jpe?g|gif|bmp|png))$ http://www.newurl.com/$1 [R=301,L,NC]

It is better to match just the image name rather than full REQUEST_URI thus removal of start of text character ^

R Flag documentation

查看更多
登录 后发表回答