.htaccess, mod_rewrite, regex with directory and filetype

馋奶兔 提交于 2019-12-12 01:57:13

问题


I want to do a rewrite with the following conditions:

  1. Directory is /images
  2. file has a .jpg, .png or .gif extension

I want to redirect to the following

/images/?file=filename.extension

This is not working:

RewriteRule /images/(.*\.jpg|png|gif) /images/?file=$1

Example:

/images/example.jpg => /images/?file=example.jpg

Thanks


回答1:


I think you need parens to group the extensions.

/images/(.*\.(jpg|png|gif))



回答2:


In the .htaccess file, the path is stripped of the leading slash so you need to start with a images/ instead of /images/. Also, you can start with a ^ to match the beginning of the path so that paths like /blahblah/images/something.gif won't get rewritten. Finally, your paren will match foo.jpg but not foo.png or foo.gif. Try this instead:

RewriteRule ^images/(.+\.(jpg|png|gif)) /images/?file=$1


来源:https://stackoverflow.com/questions/8060446/htaccess-mod-rewrite-regex-with-directory-and-filetype

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