2012-02-02 148 views
2

我很难弄清楚什么是错误。在使用CDN重定向到.htaccess中时重定向图像上的循环

只要我将我的CDN代码添加到我的.htaccess中以将静态内容重定向到CDN,就会导致这些文件发生重定向循环。

我注意到他们保持重定向到自己而不是加载文件。

我的.htaccess:

RewriteEngine On 

# Force no www 
RewriteCond %{HTTP_HOST} ^www [NC] 
RewriteRule ^.*$ http://domain.com/$0 [R=301,L] 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ 
RewriteRule ^index\.php$ http://domain.com/ [R=301,L] 

# Rewrite domain.com/p/contact to index.php?p=contact 
RewriteRule ^p/([^/]+)/?$ /index.php?p=$1 [L] 

我的CDN的代码,会导致网站:

<IfModule mod_expires.c> 
    ExpiresActive On 
    ExpiresDefault A0 

    # Set up caching for 1 week(s) 
    <FilesMatch "\.(jpe?g|gif|png|bmp|pdf|docx?|xlsx?|ppt|rar|zip|tar|gz|tgz|bz2|flv|avi|mov|wmv|mp3|wav)$"> 
     ExpiresDefault A604800 
     Header append Cache-Control "public" 
    </FilesMatch> 

    # Set up caching for 1 day(s) 
    <FilesMatch "\.(xml|txt)$"> 
     ExpiresDefault A86400 
     Header append Cache-Control "public" 
    </FilesMatch> 

    # Set up caching for 1 hour(s) 
    <FilesMatch "\.(js|css)$"> 
     ExpiresDefault A3600 
     Header append Cache-Control "proxy-revalidate" 
    </FilesMatch> 

    # Force no caching for dynamic files 
    <FilesMatch ".(php|cgi|pl|htm)$"> 
     ExpiresActive Off 
     Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform" 
     Header set Pragma "no-cache" 
    </FilesMatch> 
</IfModule> 

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{HTTP:Via} !\.s\.worldcdn\.com 
    # Flash wont work on cross-domain by default 
    RewriteCond $1   !^.swf$ [NC] 
    RewriteCond $1   "\.(jpe?g|gif|png|bmp|ico|js|css|pdf|docx?|xlsx?|ppt|rar|zip|tar|gz|tgz|bz2|flv|avi|mov|wmv|mp3|wav|xml|txt)$" [NC] 
    RewriteRule ^(.*)   http://cdn.domain.com/$1 [L,R] 
</IfModule> 

子域,CDN等都是正确设置。

也许这是CDN的问题? 欢呼你的帮助!

回答

3

从标志LApache Docs: flag_l

如果您在任一.htaccess文件或<Directory>部分使用重写规则,它具有的规则是如何处理一些了解是很重要的。这种简化的形式是,一旦处理了规则,重写的请求就会交还给URL分析引擎,以便处理它。有可能在处理重写的请求时,可能会再次遇到.htaccess文件或段,因此规则集可能会从头再次运行。最常见的情况是,如果其中一个规则导致重定向(无论是内部还是外部),则会导致请求过程重新开始。


因为,重写的请求被传递回URL解析引擎,之后从去年RewriteRule这里重定向,

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{HTTP:Via} !\.s\.worldcdn\.com 
    # Flash wont work on cross-domain by default 
    RewriteCond $1   !^.swf$ [NC] 
    RewriteCond $1   "\.(jpe?g|gif|png|bmp|ico|js|css|pdf|docx?|xlsx?|ppt|rar|zip|tar|gz|tgz|bz2|flv|avi|mov|wmv|mp3|wav|xml|txt)$" [NC] 
    RewriteRule ^(.*)   http://cdn.domain.com/$1 [L,R] 
</IfModule> 

要么
编辑这一行:

RewriteRule ^(.*)   http://cdn.domain.com/$1 [L] 

,如果你想拥有在浏览器中的URI变化均匀,加入第一RewriteCond

RewriteCond %{HTTP_HOST} !cdn\.domain\.com 
    RewriteCond %{HTTP:Via} !\.s\.worldcdn\.com 
    RewriteCond $1   !^.swf$ [NC] 
    RewriteCond $1   "\.(jpe?g|gif|png|bmp|ico|js|css|pdf|docx?|xlsx?|ppt|rar|zip|tar|gz|tgz|bz2|flv|avi|mov|wmv|mp3|wav|xml|txt)$" [NC] 
    RewriteRule ^(.*)   http://cdn.domain.com/$1 [L,R] 
+0

非常感谢! :) – rafleo 2012-02-02 16:01:45