-1

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.

12
  • 1
    Can anyone tell my why my question is being downvoted ?
    – Jeremy
    Apr 27, 2015 at 14:49
  • Your rule seems correct to me, what is your problem or question?
    – ShellFish
    Apr 27, 2015 at 15:03
  • @ShellFish thanks for replying, please see my edit for details
    – Jeremy
    Apr 27, 2015 at 15:13
  • Is this htaccess actually on the AWZ (host) server that is trying to use this rule? Apr 27, 2015 at 15:59
  • @PanamaJack yes the .htaccess is on the webserver mydomain.com while the CDN is on other domain which is in this example is in blitz.example.com.and.so.on
    – Jeremy
    Apr 27, 2015 at 16:12

1 Answer 1

1

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

2
  • Hi again, it has come to my realization that S3 is a content server instead of webserver. So i am begining to wonder whether putting .htaccess file in the root of my S3 bucket will work.
    – Jeremy
    Apr 29, 2015 at 13:57
  • Good question. stackoverflow.com/a/14095879/637404 seems to say you can't do that.
    – M'vy
    Apr 29, 2015 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.