.htaccess 文件使用手冊

一、apache開啓.htaccessjavascript

vi編輯httpd.conf文件,修改這兩個地方:php

(1) css

Options FollowSymLinks html

AllowOverride None java

改成 apache

Options FollowSymLinks 服務器

AllowOverride All app

 

(2)去掉下面的註釋 dom

LoadModule rewrite_module modules/mod_rewrite.so ide

 

二、.htaccess的寫法:

 

注意:在項目根目錄下建立,.htaccess文件 權限設置爲644

 

利用.htaccess指定事先製做好的錯誤提醒頁面。通常下,人們專門設立目錄,例如errors放置頁面。而後再.htaccess中,加入以下的指令: 

 

  ErrorDocument 404 /errors/notfound.html 

  ErrorDocument 500 /errors/internalerror.html 

 

  一條指令一行。上述第一條指令的意思是對於404,也找到所的文檔的得顯示頁面爲/errors目錄下的notfound.html頁面。不難看出語法格局爲: 

  ErrorDocument 錯誤代碼 /目錄名/名.擴展名 

 

  所提示的不多的話,沒必要專門製做頁面,直接在指令中HTML號了,例以下面例子: 

  ErrorDocument 401 「你權限訪問該頁面,請拋卻!」 

 

重寫 rewrite:

 

#rewrite ^/\??$ http://ue.alipay.com last;

 

#rewrite ^/\index.(htm|html|php)\??$ http://ue.alipay.com last;

 

#rewrite ^/diaochae_([0-9]+)\.html\??$ /admin/index.php?r=survey/view&wid=$1&page=end last;

 

#rewrite ^/feed.html\??$ /admin/?r=feedback/add last;

 

#rewrite ^/error.html\??$ /admin/index.php last;

 

#rewrite ^/adminlogin.html\??$ https://bumng.alipay.com last;

 

#rewrite ^/adminloginlocal.html\??$ http://bumng.test.alipay.net last;

 

#rewrite ^/admin.html\??$ /admin/index.php?r=survey/list last;

 

#rewrite ^/login.html\??$ /admin/index.php?r=survey/login last;

 

#rewrite ^/diaochach_([0-9]+)\.html\??$ /admin/index.php?r=survey/view&wid=$1 last;

 

#rewrite ^/diaocha_([0-9]+)\.html\??$ /admin/index.php?r=survey/view&wid=$1 last;

 

#rewrite ^/diaochae_([0-9]+)\.html&sign_from=3000$ /admin/index.php?r=survey/view&wid=$1&page=end&sign_from=3000 last;

 

#rewrite ^/diaocha_([0-9]+)\.html&sign_from=3000\??$ /admin/index.php?r=survey/view&wid=$1&sign_from=3000 last;

 

#rewrite ^/diaochach_([0-9]+)\.html&sign_from=3000$ /admin/index.php?r=survey/view&wid=$1&sign_from=3000 last;

 

 

什麼是 .htaccess 文件?

概述來講,htaccess 文件是 Apache 服務器中的一個配置文件,它負責相關目錄下的網頁配置。
經過 htaccess 文件,能夠幫咱們實現:網頁301重定向、自定義404錯誤頁面、改變文件擴展名、容許/阻止特定的用戶或者目錄的訪問、禁止目錄列表、配置默認文檔等功能。

——站長百科

 

理解 WordPress 的 htaccess

# 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 到這裏結束

使用範例

設置錯誤頁面

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

設置重定向

#從 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]

禁用文件夾列表

#若是你的文件夾沒有首頁文件,服務器會顯示文件列表,你能夠設置不顯示
IndexIgnore *
#僅不顯示 .zip/.jpg/.gif 格式的文件
IndexIgnore *.zip *.jpg *.gif

設置文件夾首頁

#防止顯示文件夾列表,當訪問文件夾時,服務器查找 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 "c7sky.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"
相關文章
相關標籤/搜索