11个实用的Apache .htaccess配置

来源:互联网 发布:mac照片和iphoto 编辑:程序博客网 时间:2024/05/29 04:58
Apache的.htaccess文件是服务器的心脏,控制着网站访问的各种规则。这里提供了10个不错的.htaccess片段能够帮助你优化你的网站,包括重定向、性能、可用性等等!
1. 强制后缀反斜杠

在URL的尾部加上反斜杠似乎对SEO有利 :)
1<IfModule mod_rewrite.c>
2 RewriteCond %{REQUEST_URI} /+[^\.]+$
3 RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
4</IfModule>
2. 防盗链
节省你宝贵的带宽吧!
1RewriteEngine On
2#Replace ?mysite\.com/ with your blog url
3RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
4RewriteCond %{HTTP_REFERER} !^$
5#Replace /images/nohotlink.jpg with your "don't hotlink" image url
6RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]
3. 重定向移动设备
加入你的网站支持移动设备访问的话,最好还是重定向移动设备的访问到专门定制的页面
01RewriteEngine On
02RewriteCond %{REQUEST_URI} !^/m/.*$
03RewriteCond %{HTTP_ACCEPT} "text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [NC,OR]
04RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
05RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
06RewriteCond %{HTTP_USER_AGENT}  "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
07RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
08RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
09RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
10RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
11RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
12RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
13#------------- The line below excludes the iPad
14RewriteCond %{HTTP_USER_AGENT} !^.*iPad.*$
15#-------------
16RewriteCond %{HTTP_USER_AGENT} !macintosh [NC] #*SEE NOTE BELOW
17RewriteRule ^(.*)$ /m/ [L,R=302]
4. 强制浏览器下载指定的文件类型
你可以强制浏览器下载某些类型的文件,而不是读取并打开这些文件,例如MP3、XLS。
1<Files *.xls>
2  ForceType application/octet-stream
3  Header set Content-Disposition attachment
4</Files>
5<Files *.eps>
6  ForceType application/octet-stream
7  Header set Content-Disposition attachment
8</Files>
5. 火狐的跨域名字体嵌入
火狐不允许嵌入一个外站的字体,下面的.htaccess片段可以绕过这个限制
1<FilesMatch "\.(ttf|otf|eot|woff)$">
2<IfModule mod_headers.c>
3    Header set Access-Control-Allow-Origin"http://yourdomain.com"
4</IfModule>
5</FilesMatch>
6. 使用.htaccess缓存 给网站提速
恐怕这个是最有用的代码片段了。这段代码能帮你极大的提高网站的速度!
01# 1 YEAR
02<FilesMatch "\.(ico|pdf|flv)$">
03Header set Cache-Control "max-age=29030400, public"
04</FilesMatch>
05# 1 WEEK
06<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
07Header set Cache-Control "max-age=604800, public"
08</FilesMatch>
09# 2 DAYS
10<FilesMatch "\.(xml|txt|css|js)$">
11Header set Cache-Control "max-age=172800, proxy-revalidate"
12</FilesMatch>
13# 1 MIN
14<FilesMatch "\.(html|htm|php)$">
15Header set Cache-Control "max-age=60, private, proxy-revalidate"
16</FilesMatch>
7. 阻止WordPress博客的垃圾评论
还在为垃圾评论头疼吗?你可以用Akismet插件来解决这个问题,但是.htaccess文件来的更直接:阻止垃圾评论机器人访问wp-comments-post.php文件。
1<IfModule mod_rewrite.c>
2RewriteEngine On
3RewriteCond %{REQUEST_METHOD} POST
4RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
5RewriteCond %{HTTP_REFERER} !.*yourdomainname.* [OR]
6RewriteCond %{HTTP_USER_AGENT} ^$
7RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]
8</IfModule>
8.重定向不同的feed格式到统一的格式
很多年前,有很多不同的feed格式,例如RSS、Atom、RDF等等。但是现在RSS已经占了绝对的主导地位。下面这段代码可以让你重定向不同的feed格式到同一个feed。这段代码可以直接在WordPress博客上使用。
1<IfModule mod_alias.c>
2 RedirectMatch301 /feed/(atom|rdf|rss|rss2)/?$ http://example.com/feed/
3 RedirectMatch301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://example.com/comments/feed/
4</IfModule>
9. 配置网站的HTML5视频
HTML5为我们带来了不用Flash的视频播放功能,但是你必须配置你的服务器来提供最新的HTML5视频播放功能。
1RewriteCond %{REQUEST_FILENAME} !-f
2RewriteCond %{REQUEST_FILENAME} !-d
3RewriteCond %{REQUEST_URI} !=/favicon.ico
4AddType video/ogg .ogv
5AddType video/ogg .ogg
6AddType video/mp4 .mp4
7AddType video/webm .webm
8AddType application/x-shockwave-flash swf
10. 记录PHP错误
在页面上显示PHP错误是很尴尬的事情,也不安全,下面这段代码可以把PHP错误记录到.log文件中而不在页面显示。
1# display no errs to user
2php_flag display_startup_errors off
3php_flag display_errors off
4php_flag html_errors off
5# log to file
6php_flag log_errors on
7php_value error_log /location/to/php_error.log
11. 在JavaScript代码中运行PHP
在JS中插入PHP代码有时候是很有用的,例如读取数据库。下面这段代码可以让你在JS中运行PHP。
1AddType application/x-httpd-php .js
2AddHandler x-httpd-php5 .js
3 
4<FilesMatch "\.(js|php)$">
5SetHandler application/x-httpd-php
6</FilesMatch>
0 0
原创粉丝点击