[PHP] .htaccess file
.htaccess文件可以做大量的事情,包括:
■ 禁止目錄瀏覽
■ 封禁特定IP地址的用戶
■ 只允許特定IP地址的用戶
■ 自定義錯誤頁面
■ 網址重寫等等
.htaccess 檔案處理其他與 Apache 相關的具體指令,像是安全性、網址永久轉向、錯誤處理…等,也因此幾乎每一個在 Apache 伺服器系統上運作的網站都會有 .htaccess 這個檔案存在。文字來源 ☞ 讓網站連結變得人性化!解析 URL Rewrite 網址重寫規則的概念與原理!
〈1〉如何產生一個 .htaccess 文件 1. 開啟記事本,新增一個文字文件,然後儲存檔名是「htaccess.txt」
2. 進入 cmd 模式
3. 下達指令:rename c:\htaccess.txt .htaccess
〈2〉實作 .htaccess 文件
# 阻止目錄瀏覽
Options All -Indexes
# 阻止存取.htaccess 文件
<files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</files>
# 阻止 User Agent 的所有請求
## .htaccess Code :: BEGIN
SetEnvIfNoCase user-Agent ^FrontPage [NC,OR]
SetEnvIfNoCase user-Agent ^Java.* [NC,OR]
SetEnvIfNoCase user-Agent ^Microsoft.URL [NC,OR]
SetEnvIfNoCase user-Agent ^MSFrontPage [NC,OR]
SetEnvIfNoCase user-Agent ^Offline.Explorer [NC,OR]
SetEnvIfNoCase user-Agent ^[Ww]eb[Bb]andit [NC,OR]
SetEnvIfNoCase user-Agent ^Zeus [NC]
Order Allow,Deny
Allow from all
Deny from env=bad_bot
## .htaccess Code :: END
#[自定義錯誤導向頁面] 401 Authorization Required 授權失敗;即是錯誤 password
ErrorDocument 401 /error/401.php
#[自定義錯誤導向頁面] 403 Forbidden 存取違規:即是你不可以讀取這個檔案
ErrorDocument 403 /error/403.php
#[自定義錯誤導向頁面] 404 Wrong page 錯誤檔案路經
ErrorDocument 404 /error/404.php
#[自定義錯誤導向頁面] 500 Internal Server Error 伺服器內部錯誤
ErrorDocument 500 /error/500.php
#網址重寫:隱藏實際檔案路徑
# BEGIN rewrite
<IfModule mod_rewrite.c>
#打開RewriteEngine
RewriteEngine On
#不顯示副檔名.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.+)$ $1.php [L,QSA]
</IfModule>
# END rewrite
〈3〉URL改寫與 Apache mod_rewrite .htaccess 在網址重寫技術上扮演著制定網址重寫具體指令的重要角色,mod_rewrite 模組會根據 .htaccess 檔案裡所記錄的網址重寫指令來執行網址重寫。
Source
☞ 【轉】16個簡單實用的.htaccess技巧☞ URL Rewrite(網址重寫)語法教學
☞ [Apache] 實用 .htaccess 腳本合集 – 優化你的網站
☞ Removing file extensions using htaccess
☞ How to Get The Most Out of Your .htaccess File
☞ 使用 .htaccess 的 URL 偽裝技術
☞ Add Security to your PHP projects using .htaccess file
☞ 11 個強化 WordPress 網站安全的 .Htaccess 設定技巧
☞ Use .htaccess file on an apache localhost server
☞ How do I rename a file to .htaccess in Windows 7?
☞ 利用.htaccess來保護主機下的目錄與檔案
☞ Download counter in PHP using .htaccess
☞ 如何透過 .htaccess 來導向錯誤網頁,網站存取被拒、網頁不存在時自動導向 404、403 等
☞ 如何在Apache環境下配置Rewrite規則
☞ Simple URL Rewriting
☞ Introduction To URL Rewriting
☞ URL rewriting with PHP
☞ Force your site to load securely with an .htaccess file

沒有留言: