.htaccess, mod_rewrite, regex with directory and f

2019-09-10 22:27发布

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

2条回答
女痞
2楼-- · 2019-09-10 22:36

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
查看更多
别忘想泡老子
3楼-- · 2019-09-10 22:43

I think you need parens to group the extensions.

/images/(.*\.(jpg|png|gif))
查看更多
登录 后发表回答