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

Jeremy
  • 2,516
  • 8
  • 46
  • 80
  • 1
    Can anyone tell my why my question is being downvoted ? – Jeremy Apr 27 '15 at 14:49
  • Your rule seems correct to me, what is your problem or question? – ShellFish Apr 27 '15 at 15:03
  • @ShellFish thanks for replying, please see my edit for details – Jeremy Apr 27 '15 at 15:13
  • Is this htaccess actually on the AWZ (host) server that is trying to use this rule? – Panama Jack Apr 27 '15 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 '15 at 16:12
  • So your .htaccess is here `http://blitz.example.com.s3-website-ap-southeast-1.amazonaws.com/.htaccess`? – Panama Jack Apr 27 '15 at 16:14
  • @PanamaJack no it is on another domain e.g. mydomain.com where from this domain i call the url – Jeremy Apr 27 '15 at 16:23
  • Then how is `RewriteCond %{HTTP_HOST} ^blitz.example.com.s3-ap-southeast-1.amazonaws.com$` ever going to match being on the main domain name .htaccess? If you put in that URL it's going to go to AWS's server. So the rule isn't going to execute. – Panama Jack Apr 27 '15 at 16:37
  • @PanamaJack suppose the .htaccess is located on mydomain.com and the url that i am trying to rewrite is in blitz.example.com.s3-ap-xxxx what do you recon i should do? – Jeremy Apr 27 '15 at 16:39
  • You can't rewrite to an external url, it will automatically do a redirect. I don't think you're going to be able to rewrite those external links unless maybe you create you own links from your main domain and do a Proxypass to AWS. Haven't tried that but could work. – Panama Jack Apr 27 '15 at 16:55
  • @PanamaJack as mentioned on this answer here : http://stackoverflow.com/a/6775323/1478789 it is actually possible. My case is almost identical to its OP – Jeremy Apr 27 '15 at 23:59
  • @JeremyRIrawan Have you tried on a different host? or look at some other regular expressions? – Book Of Zeus May 09 '15 at 21:53

1 Answers1

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

M'vy
  • 5,696
  • 2
  • 30
  • 43
  • 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 '15 at 13:57
  • Good question. http://stackoverflow.com/a/14095879/637404 seems to say you can't do that. – M'vy Apr 29 '15 at 14:02