问题
I want to do a rewrite with the following conditions:
- Directory is /images
- 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