htaccess只允许浏览器缓存子域网站

时间:2014-09-11 07:13:32

标签: php apache .htaccess caching

我在根.htaccess文件中添加了浏览器缓存,该网站正在处理多个域/子域,例如:www.domain.commt.domain.comrt.domain.com并且所有人都使用相同的代码网站..我使用了Codeigniter框架

现在caching启用所有域/子域,是否可以添加特定的域/子域进行缓存

.htaccess档案

<IfModule mod_rewrite.c>
    RewriteEngine On
    ########### TODO: deploying on subdir must rewrite this
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
    Options -Indexes

</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>
## EXPIRES CACHING ##
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access 1 year"
    ExpiresByType image/jpeg "access 1 year"
    ExpiresByType image/gif "access 1 year"
    ExpiresByType image/png "access 1 year"
    ExpiresByType text/css "access 1 month"
    ExpiresByType text/html "access 1 seconds"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##
<FilesMatch "\\.(js|css|html|htm|php|xml)$">
    SetOutputFilter DEFLATE
</FilesMatch>
<IfModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

这实际上可以使用Apache 2.2,使用环境变量。不幸的是,它不适用于mod_expires(您正在使用)。我们将改为使用mod_headers,这并没有太大的不同。

看起来有点复杂,但这是因为我们无法在逻辑上组合(AND / OR)环境变量。

# Sets an env. var. if the subdomain matches, and initializes the other env. var.
SetEnvIfNoCase HOST ^subdomain\.example\.com$ do_cache=1 is_static_file=0

# changes the value of other env. var. if extension matches (here: PNG or GIF)
SetEnvIfNoCase Request_URI .*\.(png|gif)$ is_static_file=1

# resets the first env. var. to 0 if the type didn't match
SetEnvIf is_static_file 0 do_cache=0

# now you can set arbitrary headers based on whether both conditions were met.
# (we're using nonsense "X-Success" headers here, for testing)
Header set X-Success "yes" env=do_cache
Header set X-Success "no" env=!do_cache

当您准备好上线时,请使用以下内容替换X-Success标题行:

Header set Cache-Control "max-age=290304000, public" env=do_cache

如果通过子域调用静态文件,则会激活静态文件的缓存。

您可以根据需要添加此类型的Headers或其他指令。您必须确保该指令接受env=do_cache附录HeaderExpiresByType不接受。