【问题标题】:.htaccess file not redirecting assets to other domain.htaccess 文件未将资产重定向到其他域
【发布时间】:2017-05-10 14:17:53
【问题描述】:

我正在尝试使用 .htaccess 文件中的重定向让我的 PHP 站点从另一台服务器拉取 /assets/u/ 中的图像。 .htaccess 肯定被使用了。我知道是因为我在 HTTPD 配置的站点根目录中设置了“AllowOverride All”,甚至通过故意输入不正确的语法并确认我收到内部服务器错误来测试它。但是我在 /assets/u/ 中的图像仍然没有从其他服务器中提取。下面是我的 .htaccess 文件...

SetEnv APPLICATION_ENV production

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^((www)|(draft))\.
RewriteRule ^(.*?)\/*$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# Start of attempted redirect #
RewriteCond %{REQUEST_URI} ^.*(/assets/u/).*$ [NC]
RewriteRule ^assets/(.*)$ http://assets.otherserver.com/$1 [L,R=301]
# End of attempted redirect #

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

【问题讨论】:

  • 请清楚地解释您的问题您想从另一台服务器获取图像或将特定网址重定向到另一台服务器?
  • @AbhishekGurjar 我想从另一台服务器获取图像
  • Fetch 的意思是你想显示来自其他服务器的图像,或者你想下载图像或重定向到图像?
  • @AbhishekGurjar ... 显示来自另一台服务器的图像。

标签: .htaccess redirect mod-rewrite


【解决方案1】:

我不完全理解你的问题,但这是我的第一次尝试。

我假设您使用assets/u/ 作为<img href="assets/u/test.jpg"> 标签中的链接,并且您的实际文件存储在http://assets.otherserver.com 地址中,

对于这项任务,我建议您使用mod_proxy 来显示您的图像而不重定向网址尝试使用以下规则我假设您能够在不违反其他规则的情况下安排您的 .htaccess。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/assets/u/(.+?)$
RewriteRule ^ http://assets.otherserver.com/%1 [P,L]

要使上述规则起作用,您必须在下面启用。

LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_module modules/mod_proxy.so

在 httpd.conf 中取消注释以上行。

如有任何更新,请通知我。

【讨论】:

    【解决方案2】:

    当你使用RewriteBase指令时,尝试删除第一个/

    RewriteCond %{REQUEST_URI} ^(assets/u/).*$ [NC]
    

    【讨论】:

      猜你喜欢
      • 2013-10-29
      • 2021-06-07
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      • 2015-12-12
      • 2011-05-24
      相关资源
      最近更新 更多