歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 18個有用的 .htaccess 文件使用技巧

18個有用的 .htaccess 文件使用技巧

日期:2017/3/1 12:20:31   编辑:關於Linux
.htaccess 是 Web 服務器 Apache 中特有的一個配置文件,操控著服務器上的許多行為,我們可以利用它來做許多事情,例如:設置訪問權限,網址重定向,等等。本文向大家展示18條 .htaccess 文件的使用技巧,歡迎各位收藏備用。 溫馨提示:在編輯 .htaccess 文件時,切記一定要先做好備份,因為這個文件相當重要,編輯錯了可能會出現不可想像的後果! 1.去除博客網址中的 WWW. 據說這樣做有利於 SEO。將下面的代碼加入到 .htaccess 文件中,結果會把所有帶 www. 的網址重定向到不帶 www. 的網址。需要將 ijinfa.cn 改成您的域名哦! RewriteEngine On RewriteCond %{HTTP_HOST} !^ijinfa.cn$ [NC] RewriteRule ^(.*)$ http://ijinfa.cn/$1 [L,R=301] 2.強制使用WWW.格式的網址 將 www.ijinfa.cn 改成您的域名哦! RewriteEngine On RewriteCond %{HTTP_HOST} ^ijinfa.cn [NC] RewriteRule ^(.*)$ http://www.ijinfa.cn/$1 [L,R=301] 3.設置圖片防盜鏈 RewriteEngine On #Replace ?mysite\.com/ with your blog url RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC] RewriteCond %{HTTP_REFERER} !^$ #Replace /images/nohotlink.jpg with your "don't hotlink" image url RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L] 4.重定向 WordPress 博客中的所有 feeds 到 feedburner 將 http://feedburner.com/yourfeed/ 換成您的 feedburner ID 號。 <IfModule mod_alias.c> RedirectMatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://feedburner.com/yourfeed/ RedirectMatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://feedburner.com/yourfeed/ </IfModule> 5.自定義錯誤頁面 需要您自己創建一些 html 文件,訪問者訪問不存在的頁面時就會顯示這些 html 頁面。 ErrorDocument 400 /errors/badrequest.html ErrorDocument 401 /errors/authreqd.html ErrorDocument 403 /errors/forbid.html ErrorDocument 404 /errors/notfound.html ErrorDocument 500 /errors/serverr.html 6.強制下載特定的文件 如果您的網站提供 mp3s, eps 或 xls 此類文件的下載,要是不想讓浏覽器來決定是否下載,利用如下代碼可強制下載。 <Files *.xls> ForceType application/octet-stream Header set Content-Disposition attachment </Files> <Files *.eps> ForceType application/octet-stream Header set Content-Disposition attachment </Files> 7.PHP 錯誤記錄 在您的服務器上創建一個 php_error.log 文件,再將以下代碼放入 .htaccess 文件中,記得修改第七行中的文件路徑。這是一條很有用的函數。 # display no errs to user php_flag display_startup_errors off php_flag display_errors off php_flag html_errors off # log to file php_flag log_errors on php_value error_log /location/to/php_error.log 8.從網址中去除文件的擴展名 文件擴展名對網頁開發者很有用,但對訪問的人沒用,您也不希望他們看到的,使用下面的方法將它們去掉。下面是去掉 html 擴展名的方法,可以改成 php, htm, asp 等。 RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.<span style="color: #ff0000;">html</span> -f RewriteRule ^(.*)$ $1.<span style="color: #ff0000;">html</span> # Replace html with your file extension, eg: php, htm, asp 9.禁止自動列出系統文件目錄 如果您網站某個文件目錄不存在 index 文件,服務器會自動列出當前目錄中的所有文件,通常您並不希望讓別人看到,使用下面這句代碼,可以禁止目錄被列出。 Options -Indexes 10.壓縮靜態文件,加速頁面下載 這樣會在一定程度上減少服務器帶寬消耗,加快讀者訪問速度。 AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4.0[678] no-gzip BrowserMatch bMSIE !no-gzip !gzip-only-text/html 11.給文件自動添加utf-8編碼 對於某些特定的文件,如 html, htm,css 或 js 等,有時可能會忘記在頁面中加上一條 <meta http-equiv=”Content-Type”> 編碼標簽,為防止出現編碼問題,可讓頁面自動生成這樣的標簽。 <FilesMatch "\.(htm|html|css|js)$"> ForceType 'text/html; charset=UTF-8' </FilesMatch> 12.去除 WordPress 網址中的 /category/ 目錄名稱 默認情況下, WordPress 分類目錄的永久鏈接會顯示如下: http://www.catswhocode.com/blog/category/wordpress 其中的目錄名 /category/ 毫無用處,可以去掉,只需在 .htaccess 文件中加上下面這一行(其中http://www.yourblog.com/ 是您的域名): RewriteRule ^category/(.+)$ http://www.yourblog.com/$1 [R=301,L] 將修改後的文件保存,分類目錄的鏈接形式就會變成如下: http://www.yourblog.com/blog/wordpress 13.使用浏覽器緩存 一個有效加快博客速度的方法就是,強制浏覽器緩存。在 .htaccess 文件中添加以下語句: FileETag MTime Size <ifmodule mod_expires.c> <filesmatch "\.(jpg|gif|png|css|js)$"> ExpiresActive on ExpiresDefault "access plus 1 year" </filesmatch> </ifmodule> 14.將帶發表日期和日志名稱的永久鏈接重定向到 /%postname%/ 格式 這個 /%postname%/ 就是日志名稱,也就是使用日志名稱作為永久鏈接。首先登錄 WordPress 後台,到設置→ 永久鏈接,在自定義永久鏈接這一項中填上 /%postname%/,之後您的博客永久鏈接會變成如下這樣: http://www.yourblog.com/name-of-the-post 接著下來重要的一步,利用301重定向將所有舊的鏈接全部指向上面所建新鏈接。打開 WordPress 根目錄下的 .htaccess 文件,加入以下一行語句: RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://www.domain.com/$4 搞定了! 15.拒絕非人為的發布評論 原理是禁止(機器人)非人為訪問 wp-comments-post.php 文件, 可有效防止大量的垃圾評論。將第四行的 yourblog.com 改成您的網址即可。 RewriteEngine On RewriteCond %{REQUEST_METHOD} POST RewriteCond %{REQUEST_URI} .wp-comments-post\.php* RewriteCond %{HTTP_REFERER} !.*yourblog.com.* [OR] RewriteCond %{HTTP_USER_AGENT} ^$ RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L] 16.將訪問者重定向到一個維護模式頁面 在升級博客或者修改主題的時候,讓訪客看到這些變化(特別是代碼錯誤),總不是件好事,甚至可能引發安全問題,這時,就有必要將所有訪問者引向一個指定的維護模式頁面。下面的語法中使用302重定向,將讀者引導到一個叫做“maintenance.html”的頁面,第三行中是您自己博客的IP地址。 RewriteEngine on RewriteCond %{REQUEST_URI} !/maintenance.html$ RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123 RewriteRule $ /maintenance.html [R=302,L] 17.僅允許您自己的 IP 訪問 wp-admin 目錄 除非是多作者博客,如果您使用的是靜態IP,可以設定只有您自己可以訪問 wp-admin 目錄,使用“allow from xx.xx.xxx.xx“ 這樣的格式,多個靜態IP各占一行。 AuthUserFile /dev/null AuthGroupFile /dev/null AuthName "Example Access Control" AuthType Basic <LIMIT GET> order allow, deny deny from all allow from xx.xx.xx.xx </LIMIT> 18.將特定的 IP 列入黑名單 使用“deny from xxx.xx.xxx.xxx”這樣的格式,不同的 IP 各占五行,這種方法可用於防止垃圾評論或某些您想禁止訪問的訪客。 <Limit GET POST> order allow,deny deny from 222.69.116.166 allow from all </Limit>
Copyright © Linux教程網 All Rights Reserved