.htaccess 图像 URL 的 RewriteRule [英] .htaccess RewriteRule for image URL

查看:24
本文介绍了.htaccess 图像 URL 的 RewriteRule的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有以下 .htaccess 重写规则:

At the moment i have the following .htaccess rewrite rule:

RewriteCond %{HTTP_HOST} ^blitz.example.com.s3-ap-southeast-1.amazonaws.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg [L]

我正在尝试重写以下 URL,使其指向实际 URL(请参阅下面的第二个链接):

I am trying to rewrite the following URL so that it points to the actual URL (see 2nd link below):

http://blitz.example.com.s3-website-ap-southeast-1.amazonaws.com/3478-product_list_default/green-army-combats-type-i.jpg

因为我需要创建一个指向真实实际 URL 的 RewriteRule,即:

Where as i need to create a RewriteRule that points to the real actual URL which is:

http://blitz.example.com.s3-website-ap-southeast-1.amazonaws.com/img/p/3/4/7/8/3478-product_list_default.jpg

谁能帮我解决这个问题?我真的对这个 RewriteRule 和 regex 感到困惑.

Can anyone help me with this ? I am really confused with this RewriteRule and regex.

编辑

我想使用如下网址:

http://blitz.example.com.s3-website-ap-southeast-1.amazonaws.com/3478-product_list_default/green-army-combats-type-i.jpg

并用另一个域重写它,如下所示:

and rewrite it with another domain like the followings :

http://blitz.example.com.s3-website-ap-southeast-1.amazonaws.com/img/p/3/4/7/8/3478-product_list_default.jpg

到目前为止,如果我使用以下 html 标签

So far if i use the following html tag

<img src="http://blitz.example.com.s3-website-ap-southeast-1.amazonaws.com/3478-product_list_default/green-army-combats-type-i.jpg">

返回 404

编辑 2

有趣的是,以下代码也返回 404 错误

Interestingly enough the following code also returns 404 error

<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^blitz.example.com.s3-ap-southeast-1.amazonaws.com$
RewriteRule ^(.*)([0-9]+)([0-9]+)([0-9]+)([0-9]+)-([a-zA-Z0-9_\-/.]+)/([a-zA-Z0-9_\-/.]+)$ img/p/$2/$3/$4/$5/$2$3$4$5-$6.jpg [L]
</IfModule>

编辑 3

无奈之下,我只在我的 .htaccess 上放了以下代码:

Out of desperation, i put only the following code on my .htaccess:

<IfModule mod_rewrite.c>

RewriteRule ^(.*)([0-9]+)([0-9]+)([0-9]+)([0-9]+)-([a-zA-Z0-9_\-/.]+)/([a-zA-Z0-9_\-/.]+)$ img/p/$2/$3/$4/$5/$2$3$4$5-$6.jpg [L]

</IfModule>

.htaccess 中没有其他代码,但仍然返回 404.

There is no other code in .htaccess, yet it still returns 404.

推荐答案

您正在尝试捕获太多内容.首先,重写引擎不会传递完整的 URL,而只会传递 URI,因此名称 http://blitz.example.com.s3-website-ap-southeast-1.amazonaws.com 永远不会, 曾经被任何 RewriteRule 捕获,所以忘记它.其次,您捕获图像的名称,这在我们的案例中似乎无关紧要.

You are trying to capture way to much. First the rewrite engine does not pass the full URL but only the URI, so the name http://blitz.example.com.s3-website-ap-southeast-1.amazonaws.com will never, ever be captured by any RewriteRule, so forget about it. Second, you capture the name of the image, which seems to be irrelevant in our case.

根据评论判断,该图像位于 CDN (Amazon S3) 上.如果要转换 http://blitz.example.com.s3-website-ap-southeast-1.amazonaws.com/3478-product_list_default/green-army-combats-type-i.jpg(1) 到 http://blitz.example.com.s3-website-ap-southeast-1.amazonaws.com/img/p/3/4/7/8/3478-product_list_default.jpg(2): 这是 S3 服务器的工作.为什么是这样?这是因为 (1) 的 HTTP 请求将被试图加载图像的网络浏览器直接发送到 S3 服务器.所以 .htaccess 必须放在 S3 服务器上并启用 mod_rewrite.

Judging by the comment, the image is located on a CDN (Amazon S3). If you want to convert http://blitz.example.com.s3-website-ap-southeast-1.amazonaws.com/3478-product_list_default/green-army-combats-type-i.jpg(1) to http://blitz.example.com.s3-website-ap-southeast-1.amazonaws.com/img/p/3/4/7/8/3478-product_list_default.jpg(2): it is a job for the S3 server. Why is this? It's because the HTTP request for (1) will be directly send to the S3 server by the web-browser trying to load the image. So the .htaccess has to be put on the S3 server and mod_rewrite enabled.

从好的方面来说,您的正则表达式是正确的:Regex101

On the plus side, your regex is correct : Regex101

这篇关于.htaccess 图像 URL 的 RewriteRule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆