Apache 配置:.htaccess
Apache 的 .htaccess 檔案允許使用者配置其控制的 Web 伺服器的目錄,而無需修改主配置檔案。
雖然這很有用,但重要的是要注意使用 .htaccess 檔案會降低 Apache 的速度,因此,如果您有權訪問主伺服器配置檔案(通常稱為 httpd.conf),則應在 Directory 塊中新增此邏輯。
有關 .htaccess 檔案可以執行的操作的更多詳細資訊,請參閱 Apache HTTPD 文件站點上的 .htaccess。
本文件的其餘部分將討論您可以新增到 .htaccess 中的不同配置選項及其作用。
以下大多數塊使用 IfModule 指令,僅當相應模組配置正確且伺服器已載入它時,才執行塊內的指令。這樣,如果模組未載入,我們可以避免伺服器崩潰。
重定向
有時我們需要告訴使用者某個資源已移動,無論是臨時還是永久。這就是我們使用 Redirect 和 RedirectMatch 的原因。
<IfModule mod_alias.c>
# Redirect to a URL on a different host
Redirect "/service" "http://foo2.example.com/service"
# Redirect to a URL on the same host
Redirect "/one" "/two"
# Equivalent redirect to URL on the same host
Redirect temp "/one" "/two"
# Permanent redirect to a URL on the same host
Redirect permanent "/three" "/four"
# Redirect to an external URL
# Using regular expressions and RedirectMatch
RedirectMatch "^/oldfile\.html/?$" "http://example.com/newfile.php"
</IfModule>
第一個引數的可能值列在下面。如果未包含第一個引數,則預設為 temp。
跨源資源
第一組指令控制來自伺服器的資源的 CORS(跨源資源共享)訪問。CORS 是一種基於 HTTP 標頭的機制,允許伺服器指示瀏覽器應允許載入資源的外部來源(域名、協議或埠)。
出於安全原因,瀏覽器限制了從指令碼發起的跨源 HTTP 請求。例如,XMLHttpRequest 和 Fetch API 遵循同源策略。使用這些 API 的 Web 應用程式只能請求載入應用程式的相同來源的資源,除非來自其他來源的響應包含適當的 CORS 標頭。
通用 CORS 訪問
此指令將為來自任何網站的目錄中的所有資源新增 CORS 標頭。
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
除非您在以後的配置中或在設定此指令的目錄下方的目錄的配置中覆蓋該指令,否則將滿足來自外部伺服器的每個請求,這不太可能是您想要的。
一種替代方法是明確說明哪些域名可以訪問您網站的內容。在下面的示例中,我們將訪問許可權限制到我們主站點的子域名 (example.com)。這更安全,也可能是您想要執行的操作。
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "subdomain.example.com"
</IfModule>
跨源影像
如 Chromium 部落格 中所述並在 允許跨源使用影像和畫布 中記錄的那樣,可能會導致 指紋識別 攻擊。
為了減輕這些攻擊的可能性,您應該在您請求的影像中使用 crossorigin 屬性,並在您的 .htaccess 中使用下面的程式碼片段來設定伺服器的 CORS 標頭。
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
<FilesMatch "\.(bmp|cur|gif|ico|jpe?g|a?png|svgz?|webp|heic|heif|avif)$">
SetEnvIf Origin ":" IS_CORS
Header set Access-Control-Allow-Origin "*" env=*IS_CORS*
</FilesMatch>
</IfModule>
</IfModule>
Google Chrome 的 Google Fonts 故障排除指南 告訴我們,雖然 Google Fonts 可能會在每個響應中傳送 CORS 標頭,但某些代理伺服器可能會在瀏覽器能夠使用它來呈現字型之前將其剝離。
<IfModule mod_headers.c>
<FilesMatch "\.(eot|otf|tt[cf]|woff2?)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
跨源資源計時
資源計時級別 1 規範定義了一個介面,供 Web 應用程式訪問文件中資源的完整計時資訊。
Timing-Allow-Origin 響應標頭指定允許檢視透過資源計時 API 的功能檢索到的屬性值的來源,否則這些屬性將由於跨源限制而報告為零。
如果資源沒有使用 Timing-Allow-Origin 提供服務,或者標頭在發出請求後不包含來源,則 PerformanceResourceTiming 物件的某些屬性將設定為零。
<IfModule mod_headers.c>
Header set Timing-Allow-Origin: "*"
</IfModule>
自定義錯誤頁面/訊息
Apache 允許您根據使用者收到的錯誤型別為使用者提供自定義錯誤頁面。
錯誤頁面以 URL 形式呈現。這些 URL 可以以斜槓 (/) 開頭表示本地 Web 路徑(相對於 DocumentRoot),也可以是客戶端可以解析的完整 URL。
有關更多資訊,請參閱 HTTPD 文件站點上的 ErrorDocument 指令 文件。
ErrorDocument 500 /errors/500.html
ErrorDocument 404 /errors/400.html
ErrorDocument 401 https://example.com/subscription_info.html
ErrorDocument 403 "Sorry, can't allow you access today."
錯誤預防
此設定會影響 MultiViews 對配置適用的目錄的工作方式。
MultiViews 的效果如下:如果伺服器收到對 /some/dir/foo 的請求,如果 /some/dir 已啟用 MultiViews,並且 /some/dir/foo 不存在,則伺服器讀取目錄以查詢名為 foo.* 的檔案,並有效地偽造一個型別對映,其中命名所有這些檔案,併為它們分配與客戶端按名稱請求其中一個檔案時相同的媒體型別和內容編碼。然後,它選擇最符合客戶端要求的匹配項。
該設定會停用此配置適用的目錄的 MultiViews,並防止 Apache 在同名目錄不存在時返回 404 錯誤作為重寫結果。
Options -MultiViews
媒體型別和字元編碼
Apache 使用 mod_mime 將內容元資料分配給 HTTP 響應為其選擇的內容,方法是將 URI 或檔名中的模式對映到元資料值。
例如,內容檔案的副檔名通常定義內容的 Internet 媒體型別、語言、字元集和內容編碼。此資訊傳送在包含該內容的 HTTP 訊息中,並在選擇備選方案時用於內容協商,以便在選擇要提供的多個可能內容之一時尊重使用者的偏好。
更改檔案的元資料不會更改 Last-Modified 標頭的值。因此,客戶端或代理可能仍然使用以前的標頭使用先前快取的副本。如果更改元資料(語言、內容型別、字元集或編碼),則可能需要“touch”受影響的檔案(更新其上次修改日期),以確保所有訪問者都收到更正的內容標頭。
使用正確的媒體型別(又名 MIME 型別)提供資源
將媒體型別與一個或多個副檔名關聯,以確保資源將被適當地提供。
如 HTML 規範 中所述,伺服器應為 JavaScript 資源使用 text/javascript。
<IfModule mod_mime.c>
# Data interchange
AddType application/atom+xml atom
AddType application/json json map topojson
AddType application/ld+json jsonld
AddType application/rss+xml rss
AddType application/geo+json geojson
AddType application/rdf+xml rdf
AddType application/xml xml
# JavaScript
AddType text/javascript js mjs
# Manifest files
AddType application/manifest+json webmanifest
AddType application/x-web-app-manifest+json webapp
AddType text/cache-manifest appcache
# Media files
AddType audio/mp4 f4a f4b m4a
AddType audio/ogg oga ogg opus
AddType image/bmp bmp
AddType image/svg+xml svg svgz
AddType image/webp webp
AddType video/mp4 f4v f4p m4v mp4
AddType video/ogg ogv
AddType video/webm webm
AddType image/x-icon cur ico
# HEIF Images
AddType image/heic heic
AddType image/heif heif
# HEIF Image Sequence
AddType image/heics heics
AddType image/heifs heifs
# AVIF Images
AddType image/avif avif
# AVIF Image Sequence
AddType image/avis avis
# WebAssembly
AddType application/wasm wasm
# Web fonts
AddType font/woff woff
AddType font/woff2 woff2
AddType application/vnd.ms-fontobject eot
AddType font/ttf ttf
AddType font/collection ttc
AddType font/otf otf
# Other
AddType application/octet-stream safariextz
AddType application/x-bb-appworld bbaw
AddType application/x-chrome-extension crx
AddType application/x-opera-extension oex
AddType application/x-xpinstall xpi
AddType text/calendar ics
AddType text/markdown markdown md
AddType text/vcard vcard vcf
AddType text/vnd.rim.location.xloc xloc
AddType text/vtt vtt
AddType text/x-component htc
</IfModule>
設定預設 charset 屬性
Web 上的每一部分內容都有一個字元集。大多數(如果不是全部)內容都是 UTF-8 Unicode。
使用 AddDefaultCharset 以 UTF-8 字元集提供所有標記為 text/html 或 text/plain 的資源。
<IfModule mod_mime.c>
AddDefaultCharset utf-8
</IfModule>
為特定媒體型別設定字元集
使用 mod_mime 中可用的 AddCharset 指令,將以下檔案型別與設定為 UTF-8 的 charset 引數一起提供。
<IfModule mod_mime.c>
AddCharset utf-8 .appcache \
.bbaw \
.css \
.htc \
.ics \
.js \
.json \
.manifest \
.map \
.markdown \
.md \
.mjs \
.topojson \
.vtt \
.vcard \
.vcf \
.webmanifest \
.xloc
</IfModule>
Mod_rewrite 和 RewriteEngine 指令
mod_rewrite 提供了一種根據正則表示式規則動態修改傳入 URL 請求的方法。這使您可以將任意 URL 對映到您的內部 URL 結構中的任何位置。
它支援無限數量的規則和每個規則的無限數量的附加規則條件,以提供真正靈活且強大的 URL 操作機制。URL 操作可以依賴於各種測試:伺服器變數、環境變數、HTTP 標頭、時間戳、外部資料庫查詢以及各種其他外部程式或處理程式,可用於實現細粒度的 URL 匹配。
啟用 mod_rewrite
啟用 mod_rewrite 的基本模式是使用它的所有其他任務的先決條件。
所需步驟為
- 開啟重寫引擎(這是為了使
RewriteRule指令能夠工作所必需的),如 RewriteEngine 文件中所述。 - 如果尚未啟用
FollowSymLinks選項,請啟用它。請參閱 核心選項 文件。 - 如果您的 Web 主機不允許
FollowSymlinks選項,則需要將其註釋掉或刪除,然後取消註釋Options +SymLinksIfOwnerMatch行,但請注意 效能影響。- 某些雲託管服務將要求您設定
RewriteBase。 - 請參閱 Rackspace 常見問題解答 和 HTTPD 文件。
- 根據伺服器的設定方式,您可能還需要使用
RewriteOptions指令為重寫引擎啟用某些選項。
- 某些雲託管服務將要求您設定
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
# RewriteBase /
# RewriteOptions <options>
</IfModule>
強制使用 HTTPS
這些重寫規則將從不安全的 http:// 版本重定向到 URL 的安全的 https:// 版本,如 Apache HTTPD wiki 中所述。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
</IfModule>
如果您使用 cPanel AutoSSL 或 Let's Encrypt Webroot 方法建立 TLS 證書,如果驗證請求重定向到 HTTPS,則它將無法驗證證書。啟用您需要的條件。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[\w-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
從 www. URL 重定向
這些指令將 www.example.com 重寫為 example.com。
您不應該在多個來源(帶和不帶 www)中複製內容。這可能導致 SEO 問題(重複內容),因此,您應該選擇其中一個替代方案並重定向另一個。您還應該使用規範 URL來指示搜尋引擎應該抓取哪個 URL(如果它們支援此功能)。
設定%{ENV:PROTO}變數,以允許重寫自動使用適當的方案(http或https)進行重定向。
該規則預設假設 HTTP 和 HTTPS 環境都可用於重定向。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteRule ^ - [E=PROTO:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^ - [E=PROTO:http]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
</IfModule>
在 URL 開頭插入www.
這些規則將在 URL 開頭插入www.。需要注意的是,您永遠不應該在兩個不同的 URL 下提供相同的內容。
這可能導致 SEO 問題(重複內容),因此,您應該選擇其中一個替代方案並重定向另一個。對於支援規範 URL 的搜尋引擎,您應該使用規範 URL來指示搜尋引擎應該抓取哪個 URL。
設定%{ENV:PROTO}變數,以允許重寫自動使用適當的方案(http或https)進行重定向。
該規則預設假設 HTTP 和 HTTPS 環境都可用於重定向。如果您的 TLS 證書無法處理重定向期間使用的其中一個域名,則應啟用該條件。
如果您對網站的某些部分使用“真實”子域名,則以下方法可能不是一個好主意。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteRule ^ - [E=PROTO:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^ - [E=PROTO:http]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_ADDR} !=127.0.0.1
RewriteCond %{SERVER_ADDR} !=::1
RewriteRule ^ %{ENV:PROTO}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
框架選項
以下示例傳送X-Frame-Options響應頭,其值為 DENY,通知瀏覽器不要在任何框架中顯示網頁內容,以保護網站免受點選劫持攻擊。
這可能不是每個人都適用的最佳設定。您應該閱讀有關X-Frame-Options頭的另外兩個可能值:SAMEORIGIN和ALLOW-FROM。
雖然您可以為網站的所有頁面傳送X-Frame-Options頭,但這可能存在一個缺點,即它甚至禁止對您的內容進行任何框架化(例如:當用戶使用 Google 圖片搜尋結果頁面訪問您的網站時)。
但是,您應該確保為所有允許使用者執行狀態更改操作的頁面傳送X-Frame-Options頭(例如:包含一鍵購買連結、結賬或銀行轉賬確認頁面、進行永久配置更改的頁面等)。
<IfModule mod_headers.c>
Header always set X-Frame-Options "DENY" "expr=%{CONTENT_TYPE} =~ m#text/html#i"
</IfModule>
內容安全策略 (CSP)
CSP(內容安全策略)透過設定Content Security Policy來降低跨站點指令碼和其他內容注入攻擊的風險,該策略允許網站內容的受信任來源。
沒有一個策略適合所有網站,以下示例旨在作為您修改網站的指南。
為了使您的 CSP 實現更容易,您可以使用線上CSP 頭生成器。您還應該使用驗證器來確保您的頭執行您想要的操作。
<IfModule mod_headers.c>
Content-Security-Policy "default-src 'self'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'; upgrade-insecure-requests" "expr=%{CONTENT_TYPE} =~ m#text\/(html|javascript)|application\/pdf|xml#i"
</IfModule>
目錄訪問
此指令將阻止訪問在伺服器配置的任何格式(如index.html或index.php)中不存在索引檔案的目錄。
<IfModule mod_autoindex.c>
Options -Indexes
</IfModule>
阻止訪問隱藏檔案和目錄
在 Macintosh 和 Linux 系統中,以句點開頭的檔案對使用者隱藏,但如果您知道其名稱和位置,則不會被阻止訪問。這些型別的檔案通常包含使用者首選項或實用程式的保留狀態,並且可能包含相當私密的位置,例如.git或.svn目錄。
.well-known/目錄表示“眾所周知位置”的標準(RFC 5785)路徑字首(例如:/.well-known/manifest.json、/.well-known/keybase.txt),因此,不應阻止對其可見內容的訪問。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} "!(^|/)\.well-known/([^./]+./?)+$" [NC]
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
</IfModule>
阻止訪問包含敏感資訊的文
阻止訪問某些文字編輯器可能留下的備份和原始檔,這些檔案在任何人都可以訪問時都可能構成安全風險。
更新以下示例中的<FilesMatch>正則表示式,以包含可能最終出現在生產伺服器上並可能洩露有關您網站的敏感資訊的任何檔案。這些檔案可能包括配置檔案或包含有關專案元資料的檔案等。
<IfModule mod_authz_core.c>
<FilesMatch "(^#.*#|\.(bak|conf|dist|fla|in[ci]|log|orig|psd|sh|sql|sw[op])|~)$">
Require all denied
</FilesMatch>
</IfModule>
HTTP 嚴格傳輸安全 (HSTS)
如果使用者在瀏覽器中輸入example.com,即使伺服器將其重定向到網站的安全版本,這仍然會為攻擊者提供一個機會視窗(初始 HTTP 連線)來降級或重定向請求。
以下頭確保瀏覽器僅透過 HTTPS 連線到您的伺服器,無論使用者在瀏覽器位址列中輸入什麼。
請注意,嚴格傳輸安全不可撤銷,您必須確保能夠在您在max-age指令中指定的時間內透過 HTTPS 提供服務。如果您不再擁有有效的 TLS 連線(例如,由於 TLS 證書過期),即使嘗試透過 HTTP 連線,您的訪問者也會看到錯誤訊息。
<IfModule mod_headers.c>
# Header always set
Strict-Transport-Security "max-age=16070400; includeSubDomains" "expr=%{HTTPS} == 'on'"
# (1) Enable your site for HSTS preload inclusion.
# Header always set
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" "expr=%{HTTPS} == 'on'"
</IfModule>
防止某些瀏覽器對響應進行 MIME 嗅探
- 透過將
default-src指令設定為'self'來預設限制所有提取到當前網站的來源 - 作為所有提取指令的回退。- 這很方便,因為您不必指定適用於您網站的所有提取指令,例如:
connect-src 'self'; font-src 'self'; script-src 'self'; style-src 'self'等。 - 此限制還意味著您必須明確定義允許您的網站從中載入資源的站點。否則,它將被限制為與發出請求的頁面相同的來源。
- 這很方便,因為您不必指定適用於您網站的所有提取指令,例如:
- 不允許網站上的
<base>元素。這是為了防止攻擊者更改從相對 URL 載入的資源的位置。- 如果要使用
<base>元素,則使用base-uri 'self'代替。
- 如果要使用
- 僅允許來自當前來源的表單提交:
form-action 'self'。 - 透過設定
frame-ancestors 'none',防止所有網站(包括您自己的網站)將您的網頁嵌入到例如<iframe>或<object>元素中。frame-ancestors指令有助於避免點選劫持攻擊,類似於X-Frame-Options頭。- 如果也指定了
frame-ancestors,則支援 CSP 頭的瀏覽器將忽略X-Frame-Options。
- 透過設定
upgrade-insecure-requests指令,強制瀏覽器將透過 HTTP 提供的所有資源視為透過 HTTPS 安全載入。upgrade-insecure-requests不確保頂級導航的 HTTPS。如果要強制網站本身透過 HTTPS 載入,則必須包含Strict-Transport-Security頭。
- 在能夠執行指令碼的所有響應中包含
Content-Security-Policy頭。這包括常用的檔案型別:HTML、XML 和 PDF 文件。儘管 JavaScript 檔案無法在“瀏覽上下文”中執行指令碼,但它們被包含在內以針對Web 工作執行緒。
一些較舊的瀏覽器會嘗試猜測資源的內容型別,即使它在伺服器配置中沒有正確設定。這減少了遭受驅動下載攻擊和跨源資料洩露的風險。
<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
</IfModule>
Referrer 策略
我們在能夠請求(或導航到)其他資源的資源的響應中包含Referrer-Policy頭。
這包括常用的資源型別:HTML、CSS、XML/SVG、PDF 文件、指令碼和工作執行緒。
要完全防止推薦人洩漏,請指定no-referrer值。請注意,此效果可能會對分析工具產生負面影響。
使用以下服務檢查您的Referrer-Policy
<IfModule mod_headers.c>
Header always set Referrer-Policy "strict-origin-when-cross-origin" "expr=%{CONTENT_TYPE} =~ m#text\/(css|html|javascript)|application\/pdf|xml#i"
</IfModule>
停用 TRACE HTTP 方法
TRACE方法雖然看似無害,但在某些情況下可以成功利用來竊取合法使用者的憑據。請參閱跨站點跟蹤 (XST) 攻擊和OWASP Web 安全測試指南。
現代瀏覽器現在阻止透過 JavaScript 發出的 TRACE 請求,但是,已經發現了使用瀏覽器傳送 TRACE 請求的其他方法,例如使用 Java。
如果您有權訪問主伺服器配置檔案,請改用TraceEnable指令。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^TRACE [NC]
RewriteRule .* - [R=405,L]
</IfModule>
刪除 X-Powered-By 響應頭
某些框架(如 PHP 和 ASP.NET)設定一個X-Powered-By頭,其中包含有關它們的資訊(例如,名稱、版本號)。
此頭沒有任何價值,在某些情況下,它提供的資訊可能會暴露漏洞。
<IfModule mod_headers.c>
Header unset X-Powered-By
Header always unset X-Powered-By
</IfModule>
如果可以,您應該在語言/框架級別停用X-Powered-By頭(例如:對於 PHP,您可以在php.ini中設定以下內容)。
expose_php = off;
刪除 Apache 生成的伺服器資訊頁尾
防止 Apache 在伺服器生成的文件(例如錯誤訊息、目錄列表等)中新增包含有關伺服器資訊的尾隨腳註行。有關伺服器簽名提供的內容的更多資訊,請參閱ServerSignature指令文件,以及有關配置簽名中提供的資訊的ServerTokens指令。
ServerSignature Off
修復損壞的 AcceptEncoding 頭
某些代理和安全軟體會修改或剝離Accept-EncodingHTTP 頭。有關更詳細的解釋,請參閱超越 Gzipping。
<IfModule mod_deflate.c>
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
</IfModule>
</IfModule>
</IfModule>
壓縮媒體型別
使用AddOutputFilterByType 指令壓縮所有使用以下媒體型別之一標記的輸出。
<IfModule mod_deflate.c>
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE "application/atom+xml" \
"application/javascript" \
"application/json" \
"application/ld+json" \
"application/manifest+json" \
"application/rdf+xml" \
"application/rss+xml" \
"application/schema+json" \
"application/geo+json" \
"application/vnd.ms-fontobject" \
"application/wasm" \
"application/x-font-ttf" \
"application/x-javascript" \
"application/x-web-app-manifest+json" \
"application/xhtml+xml" \
"application/xml" \
"font/eot" \
"font/opentype" \
"font/otf" \
"font/ttf" \
"image/bmp" \
"image/svg+xml" \
"image/vnd.microsoft.icon" \
"text/cache-manifest" \
"text/calendar" \
"text/css" \
"text/html" \
"text/javascript" \
"text/plain" \
"text/markdown" \
"text/vcard" \
"text/vnd.rim.location.xloc" \
"text/vtt" \
"text/x-component" \
"text/x-cross-domain-policy" \
"text/xml"
</IfModule>
</IfModule>
將副檔名對映到媒體型別
使用AddEncoding將以下檔名副檔名對映到指定的編碼型別,以便 Apache 可以使用適當的Content-Encoding響應頭提供檔案型別(這不會使 Apache 壓縮它們!)。如果這些檔案型別在沒有適當的Content-Encoding響應頭的情況下提供服務,則客戶端應用程式(例如瀏覽器)將不知道它們首先需要解壓縮響應,因此將無法理解內容。
<IfModule mod_deflate.c>
<IfModule mod_mime.c>
AddEncoding gzip svgz
</IfModule>
</IfModule>
快取過期
使用mod_expires模組以及Cache-Control和Expires頭,為資源提供遠期過期日期。
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 1 month"
# CSS
ExpiresByType text/css "access plus 1 year"
# Data interchange
ExpiresByType application/atom+xml "access plus 1 hour"
ExpiresByType application/rdf+xml "access plus 1 hour"
ExpiresByType application/rss+xml "access plus 1 hour"
ExpiresByType application/json "access plus 0 seconds"
ExpiresByType application/ld+json "access plus 0 seconds"
ExpiresByType application/schema+json "access plus 0 seconds"
ExpiresByType application/geo+json "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType text/calendar "access plus 0 seconds"
ExpiresByType text/xml "access plus 0 seconds"
# Favicon (cannot be renamed!) and cursor images
ExpiresByType image/vnd.microsoft.icon "access plus 1 week"
ExpiresByType image/x-icon "access plus 1 week"
# HTML
ExpiresByType text/html "access plus 0 seconds"
# JavaScript
ExpiresByType text/javascript "access plus 1 year"
# Manifest files
ExpiresByType application/manifest+json "access plus 1 week"
ExpiresByType application/x-web-app-manifest+json "access plus 0 seconds"
ExpiresByType text/cache-manifest "access plus 0 seconds"
# Markdown
ExpiresByType text/markdown "access plus 0 seconds"
# Media files
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType image/bmp "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
ExpiresByType image/webp "access plus 1 month"
# PNG and animated PNG
ExpiresByType image/apng "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
# HEIF Images
ExpiresByType image/heic "access plus 1 month"
ExpiresByType image/heif "access plus 1 month"
# HEIF Image Sequence
ExpiresByType image/heics "access plus 1 month"
ExpiresByType image/heifs "access plus 1 month"
# AVIF Images
ExpiresByType image/avif "access plus 1 month"
# AVIF Image Sequence
ExpiresByType image/avis "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
# WebAssembly
ExpiresByType application/wasm "access plus 1 year"
# Web fonts
# Collection
ExpiresByType font/collection "access plus 1 month"
# Embedded OpenType (EOT)
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
ExpiresByType font/eot "access plus 1 month"
# OpenType
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType font/otf "access plus 1 month"
# TrueType
ExpiresByType application/x-font-ttf "access plus 1 month"
ExpiresByType font/ttf "access plus 1 month"
# Web Open Font Format (WOFF) 1.0
ExpiresByType application/font-woff "access plus 1 month"
ExpiresByType application/x-font-woff "access plus 1 month"
ExpiresByType font/woff "access plus 1 month"
# Web Open Font Format (WOFF) 2.0
ExpiresByType application/font-woff2 "access plus 1 month"
ExpiresByType font/woff2 "access plus 1 month"
# Other
ExpiresByType text/x-cross-domain-policy "access plus 1 week"
</IfModule>