Apache的.htaccess文件是服務器的心臟,控制着網站訪問的各類規則。這裏提供了10個不錯的.htaccess片斷可以幫助你優化你的網站,包括重定向、性能、可用性等等!
1. 強制後綴反斜槓
在URL的尾部加上反斜槓彷佛對SEO有利 :)php
- <IfModule mod_rewrite.c>
- RewriteCond %{REQUEST_URI} /+[^\.]+$
- RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
- </IfModule>
2. 防盜鏈
節省你寶貴的帶寬吧!css
- RewriteEngine On
- #Replace ?mysite\.com/ with your blog url
- RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
- RewriteCond %{HTTP_REFERER} !^$
- #Replace /p_w_picpaths/nohotlink.jpg with your "don't hotlink" p_w_picpath url
- RewriteRule .*\.(jpe?g|gif|bmp|png)$ /p_w_picpaths/nohotlink.jpg [L]
3. 重定向移動設備
加入你的網站支持移動設備訪問的話,最好仍是重定向移動設備的訪問到專門定製的頁面html
- RewriteEngine On
- RewriteCond %{REQUEST_URI} !^/m/.*$
- RewriteCond %{HTTP_ACCEPT} "text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [NC,OR]
- RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
- RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
- RewriteCond %{HTTP_USER_AGENT} "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
- RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
- RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
- RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
- RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
- RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
- RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
- #------------- The line below excludes the iPad
- RewriteCond %{HTTP_USER_AGENT} !^.*iPad.*$
- #-------------
- RewriteCond %{HTTP_USER_AGENT} !macintosh [NC] #*SEE NOTE BELOW
- RewriteRule ^(.*)$ /m/ [L,R=302]
4. 強制瀏覽器下載指定的文件類型
你能夠強制瀏覽器下載某些類型的文件,而不是讀取並打開這些文件,例如MP三、XLS。java
- <Files *.xls>
- ForceType application/octet-stream
- Header set Content-Disposition p_w_upload
- </Files>
- <Files *.eps>
- ForceType application/octet-stream
- Header set Content-Disposition p_w_upload
- </Files>
5. 火狐的跨域名字體嵌入
火狐不容許嵌入一個外站的字體,下面的.htaccess片斷能夠繞過這個限制web
- <FilesMatch "\.(ttf|otf|eot|woff)$">
- <IfModule mod_headers.c>
- Header set Access-Control-Allow-Origin "http://yourdomain.com"
- </IfModule>
- </FilesMatch>
6. 使用.htaccess緩存 給網站提速
恐怕這個是最有用的代碼片斷了。這段代碼能幫你極大的提升網站的速度!數據庫
- # 1 YEAR
- <FilesMatch "\.(ico|pdf|flv)$">
- Header set Cache-Control "max-age=29030400, public"
- </FilesMatch>
- # 1 WEEK
- <FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
- Header set Cache-Control "max-age=604800, public"
- </FilesMatch>
- # 2 DAYS
- <FilesMatch "\.(xml|txt|css|js)$">
- Header set Cache-Control "max-age=172800, proxy-revalidate"
- </FilesMatch>
- # 1 MIN
- <FilesMatch "\.(html|htm|php)$">
- Header set Cache-Control "max-age=60, private, proxy-revalidate"
- </FilesMatch>
7. 阻止WordPress博客的垃圾評論
還在爲垃圾評論頭疼嗎?你能夠用Akismet插件來解決這個問題,可是.htaccess文件來的更直接:阻止垃圾評論機器人訪問wp-comments-post.php文件。windows
- <IfModule mod_rewrite.c>
- RewriteEngine On
- RewriteCond %{REQUEST_METHOD} POST
- RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
- RewriteCond %{HTTP_REFERER} !.*yourdomainname.* [OR]
- RewriteCond %{HTTP_USER_AGENT} ^$
- RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]
- </IfModule>
8.重定向不一樣的feed格式到統一的格式
不少年前,有不少不一樣的feed格式,例如RSS、Atom、RDF等等。可是如今RSS已經佔了絕對的主導地位。下面這段代碼能夠讓你重定向不一樣的feed格式到同一個feed。這段代碼能夠直接在WordPress博客上使用。api
- <IfModule mod_alias.c>
- RedirectMatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://example.com/feed/
- RedirectMatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://example.com/comments/feed/
- </IfModule>
9. 配置網站的HTML5視頻
HTML5爲咱們帶來了不用Flash的視頻播放功能,可是你必須配置你的服務器來提供最新的HTML5視頻播放功能。跨域
- RewriteCond %{REQUEST_FILENAME} !-f
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteCond %{REQUEST_URI} !=/favicon.ico
- AddType video/ogg .ogv
- AddType video/ogg .ogg
- AddType video/mp4 .mp4
- AddType video/webm .webm
- AddType application/x-shockwave-flash swf
10. 記錄PHP錯誤
在頁面上顯示PHP錯誤是很尷尬的事情,也不安全,下面這段代碼能夠把PHP錯誤記錄到.log文件中而不在頁面顯示。瀏覽器
- # 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
11. 在JavaScript代碼中運行PHP
在JS中插入PHP代碼有時候是頗有用的,例如讀取數據庫。下面這段代碼能夠讓你在JS中運行PHP。
- AddType application/x-httpd-php .js
- AddHandler x-httpd-php5 .js
- <FilesMatch "\.(js|php)$">
- SetHandler application/x-httpd-php
- </FilesMatch>