简体   繁体   English

Apache mod_rewrite和正则表达式

[英]Apache mod_rewrite and regular expression

In my HTML I want to refer to my images like this: 在我的HTML中,我想像这样引用我的图像:

/images/articles/this-is-the-page-title-dgj4klci5j4.jpg
/images/articles/thumbs/this-is-the-page-title-dgj4klci5j4.jpg
/images/gallery/this-is-the-page-title-qj3k56l6kfm.jpg

but they are really located at: 但他们真的位于:

/_pics/articles/dgj4klci5j4.jpg
/_pics/articles/thumbs/dgj4klci5j4.jpg
/_pics/gallery/qj3k56l6kfm.jpg

My .htaccess file is currently in the root directory of the site and looks something like this: 我的.htaccess文件当前位于站点的根目录中,看起来像这样:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^website\.com [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [R=permanent,L]

RewriteRule ^articles/ user/articles.php [L]
RewriteRule ^gallery/ info/gallery.php [L]

I have to do this because in the last possible moment the client decided that he wants more legible image filenames and this seems like a quick reasonable fix. 我必须这样做,因为在最后一刻,客户决定他想要更清晰的图像文件名,这似乎是一个快速合理的解决方案。

I imagine that I could redirect everything from /images to /_pics and take the page title out by just using mod_rewrite. 我想我可以将所有内容从/ images重定向到/ _pics并通过使用mod_rewrite取出页面标题。 Problem is that I'm not very good with regular expressions and I don't fully understand the way mod_rewrite works. 问题是我对正则表达式不是很好,我不完全理解mod_rewrite的工作方式。 Can I get some help? 我可以得到一些帮助吗? Thanks 谢谢

The real image filename only contains letters and numbers and can be jpg or png. 真实图像文件名只包含字母和数字,可以是jpg或png。

尝试在htaccess文件中添加此规则:

RewriteRule ^images/(.+)/[^/]+-([^/-]+)\.(jpe?g|gif|png)$ /_pics/$1/$2.$3 [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM