的.htaccess,mod_rewrite的,正则表达式用目录和文件类型文件类型、目录、正则表达式、htaccess

2023-09-02 10:10:38 作者:影子伴我久、孤独念我深

我想做一个重写具备以下条件:

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 将不会被改写。最后,你的括号将匹配 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