htaccess rewrite all image requests from a non-existent folder to folder-name.jpg

徘徊边缘 提交于 2019-12-12 03:48:55

问题


Here's what I am trying to do, for example . . .

http://www.website.com/images/folder-with-a-crazy-name/any-image-at-all.jpg

would rewrite to . . .

http://www.website.com/images/folder-with-a-crazy-name.jpg

Does this make sense?

Any help would be appreciated.


回答1:


To rewrite all jpeg image files in a folder to that folder with .jpg appended

RewriteRule ^images/(.+?)/[^/]+?\.jpg$ /images/$1.jpg [L]



回答2:


Just paste this into your .htaccess file:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^images/([a-z0-9-_]+)/any-image-at-all.jpg /images/$1.jpg

Or if you want even the other image formats to be rewritten, then just replace the third and the last line on the code above with this rule:

RewriteRule ^images/([a-z0-9-_]+)/any-image-at-all.([a-z0-9]{3}|[a-z0-9]{4}) /images/$1.$2

If the the folder images is also a variable, then try one of the following rules below:

RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)/any-image-at-all.jpg /$1/$2.jpg

If the file extension is a variable, then try this:

RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)/any-image-at-all.([a-z0-9]{3}|[a-z0-9]{4}) /$1/$2.$3


来源:https://stackoverflow.com/questions/15839426/htaccess-rewrite-all-image-requests-from-a-non-existent-folder-to-folder-name-jp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!