1、使用浏览器缓存
可以修改.htaccess文件让访问者使用浏览器缓存来优化其访问速度。
- FileETag MTime Size
- <ifmodule mod_expires.c>
- <filesmatch "\.(jpg|gif|png|CSS|js)$">
- ExpiresActive on
- ExpiresDefault "access plus 1 year"
- </filesmatch>
- </ifmodule>
或者:
- # 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>
2、设置你的WordPress防盗链
盗链是指其它网站直接使用你自己网站内的资源,从而浪费网站的流量和带宽,比如图片,上传的音乐,电影等文件。(替换mysite为自己的网址和/images/notlink.jpg为自己定制的防盗链声明图片)
- 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]
3、去除WordPress分类链接中的“/category/”前缀
默认情况下,WordPress的分类链接显示的样式为:
- http://xxx.com/blog/category/tech
其实其中的category部分没有任何意义,如果想去掉它可以修改.htaccess文件(替换yourblog为自己的网址)。
- RewriteRule ^category/(.+)$ http://www.yourblog.com/$1 [R=301,L]
4、压缩静态数据
可以修改.htaccess文件来压缩需要访问的数据(传输后在访问端解压),从而可以减少访问流量和载入时间。
- 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
5、阻止没有referrer来源链接的垃圾评论
设置.htaccess文件可以阻止大多数无Refferrer来源的垃圾评论机器人Bot Spammer。其会查询访问你网站的来源链接,然后阻止其通过wp-comments-post.php来进行垃圾评论。
- 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]
6、重定向wordpress的feed订阅地址
除了修改WordPress的模板文件来定制其输出的RSS Feed链接地址外,还可以使用.htaccess文件来进行设置(替换yourrssfeedlink为自己的Feedburner地址)。
- # temp redirect wordpress content feeds to feedburner
- <IfModule mod_rewrite.c>
- RewriteEngine on
- RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
- RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
- RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds2.feedburner.com/catswhocode [R=302,NC,L]
- </IfModule>
很多年前,有很多不同的feed格式,例如RSS、Atom、RDF等等。但是现在RSS已经占了绝对的主导地位。下面这段代码可以让你重定向不同的feed格式到同一个feed。这段代码可以直接在WordPress博客上使用。
- <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>
7、重定向日期格式的WP Permalink链接地址为Postname格式
如果你目 前的Permalink地址为/%year%/%monthnum%/%day%/%postname%/ 的格式,那么我强烈推荐你直接使用/%postname%/ ,这样对搜索引擎要舒服得多。首先你需要在WordPress的后台设置输出的Permalinks格式为/%postname%/ 。然后修改.htaccess文件来重定向旧的链接,不然别人以前收藏你的网址都会转成404哦!(替换yourdomain为自己的网址)
- RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://www.yourdomain.com/$4
8、定制访问者跳转到维护页面
当你进行网站升级,模板修改调试等操作时,最好让访问者临时 跳转到一个声明的维护页面(和404错误页面不同),来通知网站暂时无法访问,而不是留下一片空白或者什么http bad错误。(替换maintenance.html为自己定制的维护页面网址,替换123.123.123.123为自己目前的IP地址,不然你自己访 问也跳转哦)
- RewriteEngine on
- RewriteCond %{REQUEST_URI} !/maintenance.html$
- RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123
- RewriteRule $ /maintenance.html [R=302,L]
9、只允许自己的IP访问wp-admin
如果你不是团队合作Blog,最好设置只有自己能够访问WP的后台。前提是你的IP不是像我一样动态的哦。(替换xx.xx.xx.xx为自己的IP地址)
- AuthUserFile /dev/null
- AuthGroupFile /dev/null
- AuthName "Example Access Control"
- AuthType Basic
- <LIMIT GET>
- order deny,allow
- deny from all
- allow from xx.xx.xx.xx
- </LIMIT>
10、阻止指定IP的访问
如果你想要阻止指定IP的访问,来防止其垃圾评论,那么你可以创建自己的Backlist黑名单。(替换xx.xx.xx.xx为指定的IP地址)
- <Limit GET POST>
- order allow,deny
- deny from xx.xx.xx.xx
- allow from all
- </Limit>
11、强制后缀反斜杠
在URL的尾部加上反斜杠似乎对SEO有利 :)
- <IfModule mod_rewrite.c>
- RewriteCond %{REQUEST_URI} /+[^\.]+$
- RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
- </IfModule>
12、重定向移动设备
加入你的网站支持移动设备访问的话,最好还是重定向移动设备的访问到专门定制的页面
- 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]
13、强制浏览器下载指定的文件类型
你可以强制浏览器下载某些类型的文件,而不是读取并打开这些文件,例如MP3、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>
14、火狐的跨域名字体嵌入
火狐不允许嵌入一个外站的字体,下面的.htaccess片段可以绕过这个限制
- <FilesMatch "\.(ttf|otf|eot|woff)$">
- <IfModule mod_headers.c>
- Header set Access-Control-Allow-Origin "http://yourdomain.com"
- </IfModule>
- </FilesMatch>
15、置网站的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
16、记录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
17、在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>
确实是很实用的,受教了
全是代码,没看出来
对代码不太懂啊
很全面的htaccsss
第一个用得上。