开发者

URL rewriting for images with different domains

开发者 https://www.devze.com 2023-03-22 11:46 出处:网络
I want to use URLs like the followings : http://mydomain.com/320x200/server/path/to/my/image.jpg Where you can find 3 parameters to retrieve for rewriting :

I want to use URLs like the followings :

http://mydomain.com/320x200/server/path/to/my/image.jpg

Where you can find 3 parameters to retrieve for rewriting :

  1. 32开发者_JAVA百科0x200 : optional parameter, can be two numbers (like "320x200"), OR a single number (like "320x") OR empty (only "x")
  2. server : required (this is a specific parameter to find a server where image is hosted, but does not really matter for this case)
  3. path/to/my/image.jpg : required

and rewrite it with another domain like the followings :

http://myotherdomain.com/320/200/server/path/to/my/image.jpg

I tried the following rewrite rules but it is not working :

RewriteRule ^([0-9]+)x([0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9/.]+)$ htp://myotherdomain.com/$1/$2/$3/$4 [L]

RewriteRule ^([0-9]+)x/([a-zA-Z0-9]+)/([a-zA-Z0-9/.]+)$ htp://myotherdomain.com/$1/$2/$3 [L]

RewriteRule ^x/([a-zA-Z0-9]+)/([a-zA-Z0-9/.]+)$ htp://myotherdomain.com/$1/$2 [L]

Why is it not working ?

The 3 regex are working when tested through a website like regexplanet.com

I tried to clear browser cache, restart Apache, remove cookies, ... still not working !

Thanks for your help


Edit :

Finally, the problem was that my .htaccess file was not correctly saved (don't know why).

I just closed and opened the .htaccess again, everything ok !


You should divide the work by grouping. Don't bother converting x to / just don't capture it.

RewriteRule ^([0-9]+)x([0-9]+/)([a-zA-Z0-9]+)/([a-zA-Z0-9/.]+)$ http://myotherdomain.com/$1/$2$3/$4 [L]

RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9/.]+)$ http://myotherdomain.com/$1/$2 [L]
  • ([0-9]+) captures your two resolutions parameters individually.
  • (?:....)? is a non capturing grouping rendered optional.

Edit:

The optionality is a trap. Don't use it -> second rule.

If the other domain end up being the same domain:

RewriteCond %{REQUEST_URI} ![0-9]+/[0-9]+.*$

Will prevent loops.


I haven't tried it, but i hope it works:

RewriteRule ^(([0-9]+)(x)([0-9]+)/|)([a-zA-Z0-9]+)\/([a-zA-Z0-9_\/\.-]+)$ htp://myotherdomain.com/$2$3$4$5$6/$7 [L]
$2 = 320 or empty
$3 = 'x' or empty
$4 = 200 or empty
$5 = '/' or empty
$6 = 'server'
$7 = path to image
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号