Apache Web 服務(wù)器可以通過 .htaccess 文件來操作各種信息,這是一個(gè)目錄級(jí)配置文件的默認(rèn)名稱,允許去中央化的 Web 服務(wù)器配置管理。可用來重寫服務(wù)器的全局配置。該文件的目的就是為了允許單獨(dú)目錄的訪問控制配置,例如密碼和內(nèi)容訪問。
下面是 21 個(gè)非常有用的 .htaccess 配置的提示和技巧:
1. 定制目錄的 Index 文件
DirectoryIndex index.html index.php index.htm
你可以使用上面的配置來更改目錄的默認(rèn)頁面,例如你將這個(gè)腳本放在 foo 目錄,則用戶請(qǐng)求 /foo/ 時(shí)候就會(huì)訪問 /foo/index.html。
2. 自定義錯(cuò)誤頁
ErrorDocument 404 errors/404.html
當(dāng)用戶訪問頁面報(bào)錯(cuò)時(shí),例如頁面找不到你希望顯示自定義的錯(cuò)誤頁面,你可以通過這種方法來實(shí)現(xiàn)。或者是動(dòng)態(tài)的頁面:
ErrorDocument 404 /psych/cgi-bin/error/error?404
3. 控制訪問文件和目錄的級(jí)別
.htaccess 經(jīng)常用來限制和拒絕訪問某個(gè)文件和目錄,例如我們有一個(gè) includes 文件夾,這里存放一些腳本,我們不希望用戶直接訪問這個(gè)文件夾,那么通過下面的腳本可以實(shí)現(xiàn):
# no one gets in here!
deny from all
上述腳本是拒絕所有的訪問,你也可以根據(jù)IP段來拒絕:
# no nasty crackers in here!
order deny,allow
deny from all
allow from 192.168.0.0/24
# this would do the same thing..
#allow from 192.168.0
一般這些方法是通過防火墻來處理,但在一個(gè)生產(chǎn)環(huán)境中的服務(wù)器來說,這樣的調(diào)整非常方便。
有時(shí)候你只是想禁止某個(gè)ip訪問:
# someone else giving the ruskies a bad name..
order allow,deny
deny from 83.222.23.219
allow from all
4. 修改環(huán)境變量
環(huán)境變量包含了服務(wù)器端 CGI 的一些擴(kuò)展信息,可使用 SetEnv 和 UnSetEnv 進(jìn)行設(shè)置以及取消設(shè)置.
SetEnv SITE_WEBMASTER "Jack Sprat"
SetEnv SITE_WEBMASTER_URI mailto:Jack.Sprat@characterology.com
UnSetEnv REMOTE_ADDR
5. 301 重定向
如果你希望某個(gè)頁面跳轉(zhuǎn)到新的頁面:
Redirect 301 /old/file.html http://yourdomain.com/new/file.html
下面可以實(shí)現(xiàn)對(duì)整個(gè)路徑的重定向
RedirectMatch 301 /blog(.*) http://yourdomain.com/$1
6. 通過 .htaccess 實(shí)現(xiàn)緩存策略
通過設(shè)置在瀏覽器上緩存靜態(tài)文件可以提升網(wǎng)站的性能:
# year
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
Header unset Last-Modified
</FilesMatch>
#2 hours
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>
<FilesMatch "\.(js|css)$">
SetOutputFilter DEFLATE
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>
7. 使用 GZIP 對(duì)輸出進(jìn)行壓縮
在 .htaccess 中添加下面的代碼可以將所有的 css、js 和 html 使用 GZIP 算法壓縮:
<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>
使用上面代碼的前提是啟用 mod_gzip 模塊,你可以使用下面腳本來判斷 Web 服務(wù)器是否提供 mod_deflate 支持:
<Location>
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \
\.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary
</Location>
如果 Web 服務(wù)器不支持 mod_deflate ,那么可使用下面方法:
<FilesMatch "\.(txt|html|htm|php)">
php_value output_handler ob_gzhandler
</FilesMatch>
更多關(guān)于壓縮的內(nèi)容請(qǐng)閱讀: Compressing PHP, CSS, JavaScript(JS).
8. 強(qiáng)制要求使用 HTTPS 訪問
通過以下腳本可以強(qiáng)制整個(gè)網(wǎng)站必須使用 https 方式訪問:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
9. URL 重寫
例如要將 product.php?id=12 重寫為 product-12.html
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
將 product.php?id=12 重寫為 product/ipod-nano/12.html
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
重定向沒有 www 到有 www 的 URL 地址:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^viralpatel\.net$
RewriteRule (.*) http://www.viralpatel.net/$1 [R=301,L]
重寫 yoursite.com/user.php?username=xyz 到 yoursite.com/xyz
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
重定向某個(gè)域名到一個(gè) public_html 里新的子文件夾:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1