【问题标题】:Mod rewrite redirection to another domain if file not exist如果文件不存在,Mod 重写重定向到另一个域
【发布时间】:2012-10-05 14:53:30
【问题描述】:

我想要做什么:

如果文件不存在,我需要将对文件的请求重定向到另一个域。例如:

http://www.mydomain.com/js/foo.js 

重定向到(如果不存在)

http://www.myanotherdomain.com/js/foo.js

我做什么:

我在 htaccess 的末尾写了下一行,但它们重定向了所有!

RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(.*)$ http://www.myanotherdomain.com/$1 [L,NC]  

在这些行之前,我有很多这样的行(我正在使用 MVC (Model, View,Controller)):

RewriteRule ^car/brand/?$ ?controller=Car&action=viewBrand [L,NC]  

会发生什么:

它适用于不存在的文件,但似乎与 MVC 规则不兼容。这些规则必须匹配然后停止评估规则,因为 de "L" 标志。但它似乎继续评估规则并最终评估重定向规则。结果是这样的:

http://www.mydomain.com/car/brand/

http://www.myanotherdomain.com/?controller=Car&action=viewBrand

谁能帮帮我?

非常感谢,

乔纳森

【问题讨论】:

    标签: mod-rewrite redirect


    【解决方案1】:

    试试这个:

    RewriteCond %{REQUEST_FILE} !-f
    RewriteRule ^(.*)$ http://www.myanotherdomain.com/$1 [QSA,R,L]
    

    另请参阅:mod rewrite directory if file/folder not found

    【讨论】:

    • 感谢您的超快速回答,但结果是一样的:S
    • 刚刚改了再试一次,我不是专家,但我会尽力提供帮助:) 也许我链接的那篇文章可能会有更多帮助
    【解决方案2】:

    尝试将这些规则放在您的 MVC 规则之后:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)\.(js|png|jpe?g|gif|css|html?)$ http://www.myanotherdomain.com/$1.$2 [L,R,NC]
    

    如果您想要所有请求,而不仅仅是脚本和图像等静态内容,请将RewriteRule 行更改为:

    RewriteRule ^(.*)$ http://www.myanotherdomain.com/$1 [L,R,NC]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-18
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 2013-09-02
      • 2012-08-24
      • 1970-01-01
      相关资源
      最近更新 更多