htaccess的重写规则的图片网址 [英] .htaccess RewriteRule for image URL

查看:617
本文介绍了htaccess的重写规则的图片网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 的RewriteCond%{HTTP_HOST} ^ blitz.example.com.s3-ap-southeast-1.amazonaws.com $
重写规则^([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(见下面第2个链接):

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

在哪里,因为我需要创建一个重写规则指向哪个才是真正的实际的URL:

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

谁能帮助我?我真的很困惑这个重写规则和正则表达式。

修改

我想使用像以下网址:

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

和使用类似如下的另一个域改写它:

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

到目前为止,如果我使用下面的HTML标签

 &LT; 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

有趣的是下面的code也将返回404错误

 &LT; IfModule mod_rewrite.c&GT;
的RewriteCond%{HTTP_HOST} ^ blitz.example.com.s3-ap-southeast-1.amazonaws.com $
重写规则^([0-9] +)([0-9] +)([0-9] +)([0-9] +)(*) - ([A-ZA-Z0-9 _ \  - /.]+)/([a-zA-Z0-9_\-/.]+)$ IMG / P / $ 2 / $ 3 / $ 4 / $ 10 / $ 2 $ 3 $ 4 $ 5- $ 6.JPG [L]
&LT; / IfModule&GT;
 

修改3

出于绝望,我只放下面的code对我的.htaccess:

 &LT; IfModule mod_rewrite.c&GT;

重写规则^([0-9] +)([0-9] +)([0-9] +)([0-9] +)(*) - ([A-ZA-Z0-9 _ \  - /.]+)/([a-zA-Z0-9_\-/.]+)$ IMG / P / $ 2 / $ 3 / $ 4 / $ 10 / $ 2 $ 3 $ 4 $ 5- $ 6.JPG [L]

&LT; / IfModule&GT;
 

有没有其他的code在.htaccess,但它仍然会返回404。

解决方案

正在试图捕捉办法不多。首先重写引擎不通过完整的URL,但只有URI,故得名 http://blitz.example.com.s3-website-ap-southeast-1.amazonaws.com 将永远不会被任何重写规则,所以忘掉它被捕获。其次,你捕捉到的图像,这似乎是无关紧要在我们的例子中的名称。

在注释来看,图像位于一个CDN(亚马逊S3)。如果你想转换<$c$c>http://blitz.example.com.s3-website-ap-southeast-1.amazonaws.com/3478-product_list_default/green-army-combats-type-i.jpg(1)至<$c$c>http://blitz.example.com.s3-website-ap-southeast-1.amazonaws.com/img/p/3/4/7/8/3478-product_list_default.jpg(2):它是为S3的服务器的工作。为什么是这样?这是因为对于HTTP请求(1)将被直接发送到S3服务器上的Web浏览器尝试加载图像。因此,的.htaccess ,必须把S3服务器上,的mod_rewrite 启用。

从积极的一面,你的正则表达式是正确的: Regex101

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]

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

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

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

EDIT

I want to use URLs like the followings :

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

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">

it returns 404

EDIT 2

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>

EDIT 3

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>

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

解决方案

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.

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.

On the plus side, your regex is correct : Regex101

这篇关于htaccess的重写规则的图片网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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