基于.htaccess文件中的模式显示特定图像(Displaying a certain image based on a pattern in an .htaccess file)

编程入门 行业动态 更新时间:2024-10-22 21:29:39
基于.htaccess文件中的模式显示特定图像(Displaying a certain image based on a pattern in an .htaccess file)

我有一系列图像,我想重新命名为SEO目的。 但是,我有超过6000张图像,所以我认为必须轻松重写我的.htaccess文件。

当前图像当前使用以下模式命名:

http://www.mydomain.com/images/small/00005712.jpg

http://www.mydomain.com/images/medium/00005712.jpg

http://www.mydomain.com/images/large/00005712.jpg

我希望能够使用命名方案调用它们,例如:

http://www.mydomain.com/5712s-keyword-phrase.jpg

http://www.mydomain.com/5712m-keyword-phrase.jpg

http://www.mydomain.com/5712l-keyword-phrase.jpg

这将允许我更改显示:

<img src="http://www.mydomain.com/images/large/00005712.jpg" />

<img src="http://www.mydomain.com/5712l-keyword-phrase.jpg" />

重写希望允许我简单地更改我的php文件中的img src属性的模式。 使用新名称在整个站点上的img src属性实际上将原始图像呈现给屏幕。

所有文件名都有8位数字名称,左边是0。

另请注意,如果您知道如何使用左侧的关键字 - 短语和最后的数字来完成此操作,例如http://www.mydomain.com/keyword-phrase-5712s.jpg更好。

如果我没有提供足够的细节,请告诉我我可以做些什么来澄清。 任何有关此事的帮助将不胜感激。

I have a series of images that I would like to rename for SEO purposes. However, I have over 6,000 images so I thought that there must be an easy rewrite for my .htaccess file.

The current images are currently named using the following pattern:

http://www.mydomain.com/images/small/00005712.jpg

http://www.mydomain.com/images/medium/00005712.jpg

http://www.mydomain.com/images/large/00005712.jpg

I would like to be able to call them with a naming scheme such as:

http://www.mydomain.com/5712s-keyword-phrase.jpg

http://www.mydomain.com/5712m-keyword-phrase.jpg

http://www.mydomain.com/5712l-keyword-phrase.jpg

This will allow me to change the display from:

<img src="http://www.mydomain.com/images/large/00005712.jpg" />

to

<img src="http://www.mydomain.com/5712l-keyword-phrase.jpg" />

The rewrite would hopefully allow me to simply change the pattern on the img src attribute in my php file. The img src attributes across the entire site with the new name would actually present the original images to the screen.

All of the filenames have 8 digit numeric names padded with 0's on the left.

Also note that if you know of a way to do this with the keyword-phrase on the left and the number being at the end, such like http://www.mydomain.com/keyword-phrase-5712s.jpg that it would be even better.

If I've not provided enough details, please let me know what I can do to clarify. Any help on this matter would be greatly appreciated.

最满意答案

如果将文件夹重命名为sm和l,则可以使用1个规则集

RewriteCond %{REQUEST_URI} (.*)\.jpg$ RewriteRule ^0*([1-9])(\d*)(s|m|l)(.*)$ /images/$3/$1$2.jpg

否则你需要3

RewriteCond %{REQUEST_URI} (.*)\.jpg$ RewriteRule ^0*([1-9])(\d*)s(.*)$ /images/small/$1$2.jpg RewriteCond %{REQUEST_URI} (.*)\.jpg$ RewriteRule ^0*([1-9])(\d*)m(.*)$ /images/medium/$1$2.jpg RewriteCond %{REQUEST_URI} (.*)\.jpg$ RewriteRule ^0*([1-9])(\d*)l(.*)$ /images/large/$1$2.jpg

编辑

根据下面的评论,扔掉文件名前面的SEO术语,使用:

RewriteRule ^.*(\d+)s\.(jpg|gif|png)$ /images/small/$1.$2 RewriteRule ^.*(\d+)m\.(jpg|gif|png)$ /images/medium/$1.$2 RewriteRule ^.*(\d+)l\.(jpg|gif|png)$ /images/large/$1.$2

开头的。*会丢弃图像数字前面的任何前导字符。 由于前导零现在包含在文件URL中,我们可以使用(\ d +)匹配(s | m | l)文件夹指示符和图像文件扩展名之前的一个或多个数字。

If you rename your folders to s m and l you can use 1 ruleset

RewriteCond %{REQUEST_URI} (.*)\.jpg$ RewriteRule ^0*([1-9])(\d*)(s|m|l)(.*)$ /images/$3/$1$2.jpg

otherwise you need 3

RewriteCond %{REQUEST_URI} (.*)\.jpg$ RewriteRule ^0*([1-9])(\d*)s(.*)$ /images/small/$1$2.jpg RewriteCond %{REQUEST_URI} (.*)\.jpg$ RewriteRule ^0*([1-9])(\d*)m(.*)$ /images/medium/$1$2.jpg RewriteCond %{REQUEST_URI} (.*)\.jpg$ RewriteRule ^0*([1-9])(\d*)l(.*)$ /images/large/$1$2.jpg

Edit

per comment below, to throw away SEO terms preceding the file name, use:

RewriteRule ^.*(\d+)s\.(jpg|gif|png)$ /images/small/$1.$2 RewriteRule ^.*(\d+)m\.(jpg|gif|png)$ /images/medium/$1.$2 RewriteRule ^.*(\d+)l\.(jpg|gif|png)$ /images/large/$1.$2

the .* at the beginning throws away any leading characters before the image digits. Since the leading zeros are included in the file url now, we can just use (\d+) to match 1 or more digits immediately preceding the (s|m|l) folder indicator and the image file extension.

更多推荐

本文发布于:2023-07-23 00:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225193.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   模式   文件   htaccess   Displaying

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!