在.htaccess中使用CDN的重定向时,重定向循环上的图像重定向、图像、htaccess、CDN

2023-09-02 00:46:06 作者:一别便不再见

我有一个很难搞清楚什么是错的。

I am having a hard time figuring out what is wrong.

当我加入我的CDN的code到我的.htaccess重定向静态内容的CDN,它会导致对这些文件重定向循环。

As soon as I add my CDN's code to my .htaccess to redirect static content to the CDN, it results in a redirect loop on those files.

我注意到他们不断重定向到自己,而不是加载该文件。

I noticed they keep redirecting to themselves instead of loading the file.

我的.htaccess:

My .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的code,打破了网站:

My CDN's code that breaks the site:

<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等都是正确设置。

Subdomains, CDN etc. are all set up correctly.

也许这是与CDN的问题吗? 干杯您的帮助​​!

Maybe it's a problem with the CDN? Cheers for your help!

推荐答案

这标志:阿帕奇文档:flag_l :

From Flag L: Apache Docs: flag_l :

如果您使用的是重写规则在任的.htaccess 文件或&LT;目录&GT; 部分,这一点很重要有怎样的规则处理一些了解。这样做的简化形式是,一旦规则已经被处理,则重写的请求被交还给URL解析引擎做它可以与它。这可能是因为所重写的请求的处理,.htaccess文件或部分可以再次遇到,因此规则集可再​​次从开始运行。无论是内部还是外部 - - 最常见的是如果一个规则导致重定向会出现这种情况,导致请求过程重新来过

If you are using RewriteRule in either .htaccess files or in <Directory> sections, it is important to have some understanding of how the rules are processed. The simplified form of this is that once the rules have been processed, the rewritten request is handed back to the URL parsing engine to do what it may with it. It is possible that as the rewritten request is handled, the .htaccess file or section may be encountered again, and thus the ruleset may be run again from the start. Most commonly this will happen if one of the rules causes a redirect - either internal or external - causing the request process to start over.

一直以来,重写的请求被传递回URL解析引擎,从最后一个重定向后重写规则这里,

Since, rewritten request is handed back to the URL parsing engine, after redirect from the last RewriteRule here,

<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加速的原理

either edit this line to :

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

如果你想拥有的URI的变化在浏览器为好,添加的第一个的RewriteCond

if you want to have the URI change at the browser as well, add the first 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]