2014-09-11 75 views
1

我已經添加根.htaccess文件瀏覽器緩存,網站上多個域的工作/像子域:www.domain.commt.domain.comrt.domain.com和相同的代碼使用的所有網站。我已經使用Codeigniter框架htaccess的使瀏覽器緩存只有子域名網站

現在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> 
添加特定的域/子域

感謝您的幫助

+0

您是否可以訪問'httpd.conf',更具體地說是您的'VirtualHost'還是唯一的選項是'.htaccess'?你的apache,2.2+或2.4+版本是什麼? – Prix 2014-09-17 06:36:13

+0

我可以訪問'httpd.conf',而且apache使用的是'2.2'版本' – Girish 2014-09-17 08:43:20

+0

感謝您的回覆,我已經創建了一個虛擬主機''通配符子域,因爲子域名站點不是固定的,這些發佈/從網站管理員發佈..我想申請緩存所有子域網站和跳過網站管理員(域名)緩存,但問題是主站點和子域網站代碼相同..我們可以使用另一個' .htaccess'爲子域**(*。domains.com)**,任何運氣。 – Girish 2014-09-17 19:18:13

回答

1

這實際上可以與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附錄(Header確實,ExpiresByType沒有)。

+0

感謝您的回答,我會嘗試一下..我們將如何在'Header set..'中設置'content-type',因爲我想立即過期content-type('text/html,application/json/text/json ') – Girish 2014-09-17 19:21:12

+0

只需通過分機即可。對於靜態文件,它不應該是一個問題。唯一不能使用的情況是寫入自己頭的動態(PHP)文件。但他們也應該能夠控制自己的緩存。 – lxg 2014-09-17 20:59:11