首先簡單介紹一下,.htaccess文件是Apache服務器中的一個配置文件(Nginx服務器沒有),它負責相關目錄下的網頁配置。通過對.htaccess文件進行設置,可以幫我們實現:網頁301重定向、自定義400/403/404/500錯誤頁面、改變文件擴展名、允許/阻止指定IP用戶訪問、禁止目錄列表、配置默認文檔等功能,可以說是功能非常強大,下面就給大家介紹一下最常用的幾個功能的設置方法。
設置網站錯誤頁面
ErrorDocument 400 /error_pages/400.html
ErrorDocument 401 /error_pages/401.html
ErrorDocument 403 /error_pages/403.html
ErrorDocument 404 /error_pages/404.html
ErrorDocument 500 /error_pages/500.html
設置網頁301重定向
#從 old_dir 目錄重定向到 new_dir 目錄
Redirect /old_dir/ http://www.yourdomain.com/new_dir/index.html
#把通過二級目錄訪問的請求301重定向到二級域名
RedirectMatch 301 /dir/(.*) http://dir.yourdomain.com/$1
禁止指定IP段用戶的訪問
#禁止 IP 為 255.0.0.0 和 123.45.6.區段的 IP 訪問
order allow,deny
deny from 255.0.0.0
deny from 123.45.6.
allow from all
禁止指定來源網頁訪問
#禁止從 otherdomain.com 和 anotherdomain.com 的來源訪問
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} otherdomain\.com [NC,OR]
RewriteCond %{HTTP_REFERER} anotherdomain\.com
RewriteRule .* – [F]
圖片防盜鏈設置
#從本站以外的域名訪問圖片,一律顯示 feed.jpg
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]
RewriteRule \.(gif|jpg|png)$ http://www.yourdomain.com/feed.jpg [R,L]
設置文件夾首頁
#防止顯示文件夾列表,當訪問文件夾時,服務器會查找index.html,並將其做為首頁文件,如不存在依次向后查找
DirectoryIndex index.html index.cgi index.php
設置多媒體文件為可下載而非播放
AddType application/octet-stream .mp3 .mp4
自定義HTTP報頭
Header set X-Pingback “http://www.yourdomain.com/xmlrpc.php”
Header set article-by “yourdomain.com”
設置文件過期時間 Cache Control
# 啟用有效期控制
ExpiresActive On
# gif/png/jpg 有效期為1個月
ExpiresByType image/gif “access plus 1 month”
ExpiresByType image/png “access plus 1 month”
ExpiresByType image/jpg “access plus 1 month”
# js/css 有效期為1星期
ExpiresByType text/javascript “access plus 1 week”
ExpiresByType text/css “access plus 1 week”
WordPress建站程序偽靜態代碼
# BEGIN WordPress #這是一行注釋,表示 WordPress 的 htaccess 從這里開始
#如果Apache加載了mod_rewrite.c模塊,則運行以下代碼
RewriteEngine On #啟用 mod_rewrite 引擎
RewriteBase / #設置目錄重寫的基准URL為 /
RewriteRule ^index\.php$ – [L] #如果請求路徑是 index.php,停止重寫操作(避免死循環)
RewriteCond %{REQUEST_FILENAME} !-f #如果請求的不是一個文件,繼續處理
RewriteCond %{REQUEST_FILENAME} !-d #如果請求的不是一個目錄,繼續處理
RewriteRule . /index.php [L] #把所有的請求指向 /index.php
#結束 IfModule
# END WordPress #WordPress 的 htaccess 到這里結束
Discuz x3/x3.1通用偽靜態代碼
#如果Apache加載了mod_rewrite.c模塊,則運行以下代碼
RewriteEngine On
RewriteBase /discuz
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^topic-(.+)\.html$ portal.php?mod=topic&topic=$1&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^article-([0-9]+)-([0-9]+)\.html$ portal.php?mod=view&aid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^forum-(\w+)-([0-9]+)\.html$ forum.php?mod=forumdisplay&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^group-([0-9]+)-([0-9]+)\.html$ forum.php?mod=group&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^space-(username|uid)-(.+)\.html$ home.php?mod=space&$1=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^blog-([0-9]+)-([0-9]+)\.html$ home.php?mod=space&uid=$1&do=blog&id=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^archiver/(fid|tid)-([0-9]+)\.html$ archiver/index.php?action=$1&value=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ plugin.php?id=$1:$2&%1
#結束 IfModule