Apache mod_rewrite和正则表达式

在我的HTML中我想引用我的图像:</ p>

  <代码> /images/articles/this-is-the-page-title-dgj4klci5j4.jpg
/images/articles/thumbs/this-is-the-page-title-dgj4klci5j4.jpg
/images/gallery/ 这是-page-title-qj3k56l6kfm.jpg
</ code> </ pre>

但它们的确位于:</ p>

  / _ pics / articles / dgj4klci5j4.jpg 
/ _pics / articles / thumbs / dgj4klci5j4.jpg
/ _pics / gallery / qj3k56l6kfm.jpg
</ code> </ pre>

我的。 htaccess文件当前位于站点的根目录中,如下所示:</ p>

  RewriteEngine On 

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

RewriteRule ^ articles / user / articles.php [L]
RewriteRule ^ gallery / info / gallery.php [L]
</ code> </ pre>

我必须这样做,因为在最后可能的时刻,客户决定他想要更清晰的图像文件名,这似乎是一个快速合理的f ix。</ p>

我想我可以将所有内容从/ images重定向到/ _pics并通过使用mod_rewrite取出页面标题。 问题是我对正则表达式不是很好,我不完全理解mod_rewrite的工作方式。 我可以得到一些帮助吗? 谢谢</ p>

真实图像文件名只包含字母和数字,可以是jpg或png。</ p>
</ div>

展开原文

原文

In my HTML I want to refer to my images like this:

/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:

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. Problem is that I'm not very good with regular expressions and I don't fully understand the way mod_rewrite works. Can I get some help? Thanks

The real image filename only contains letters and numbers and can be jpg or png.

1个回答



尝试在htaccess文件中添加此规则:</ p>

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

展开原文

原文

Try adding this rule in the htaccess file:

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

dou29106
dou29106 谢谢,工作就像一个魅力
大约 8 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问