的.htaccess,mod_rewrite的,正则表达式用目录和文件类型 [英] .htaccess, mod_rewrite, regex with directory and filetype
本文介绍了的.htaccess,mod_rewrite的,正则表达式用目录和文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做一个重写具备以下条件:
I want to do a rewrite with the following conditions:
- 目录是/图片
- 在文件中有一个JPG格式,png格式或.gif扩展名
我要重定向到以下
/images/?file=filename.extension
这是行不通的:
RewriteRule /images/(.*\.jpg|png|gif) /images/?file=$1
例如:
/images/example.jpg
=> /images/?file=example.jpg
感谢
推荐答案
在.htaccess文件,该路径被剥离的斜线,所以你需要开始使用图片/
而不是 /图片/
。此外,您还可以用开始 ^
匹配路径的开头,这样像 /blahblah/images/something.gif $ C路径$ C>将不会被改写。最后,你的括号将匹配
foo.jpg
而不是 foo.png
或 foo.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
这篇关于的.htaccess,mod_rewrite的,正则表达式用目录和文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文