简体   繁体   中英

.htaccess, mod_rewrite, regex with directory and filetype

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

我认为您需要括号将扩展分组。

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

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

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