具有不同域的图像的URL重写 [英] URL rewriting for images with different domains

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

问题描述

我想使用如下网址:

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

您可以在其中找到 3个参数以进行重写:

Where you can find 3 parameters to retrieve for rewriting :

  1. 320x200:可选参数,可以是两个数字(例如"320x200"),也可以是单个数字(例如"320x")或为空(仅"x")
  2. server:必需(这是找到托管图像的服务器的特定参数,但在这种情况下并不重要)
  3. path/to/my/image.jpg:必需
  1. 320x200 : 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]

为什么不起作用?

通过regexplanet.com之类的网站进行测试时,这3个正则表达式可以正常工作

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

我试图清除浏览器缓存,重新启动Apache,删除cookie,...仍然无法正常工作!

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

感谢您的帮助

最后,问题是我的.htaccess文件未正确保存(不知道为什么).

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

我刚刚关闭并再次打开.htaccess,一切正常!

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

推荐答案

您应按分组划分工作.不必费心将x转换为/,只是不要捕获它.

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]+)分别捕获您的两个分辨率参数.
  • (?:....)?是一个非捕获性分组,它是可选的.
    • ([0-9]+) captures your two resolutions parameters individually.
    • (?:....)? is a non capturing grouping rendered optional.
    • 可选项是一个陷阱.不要使用它->第二条规则.

      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]+.*$
      

      将防止循环.

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

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