.htaccess设置301跳转及常用技巧
RewriteEngine on
RewriteRule ^(.*)$ http://www.zhibushi.com/$1 [R=301,L]
网站换站换域名时使用,可以将整站权重转移,使新站能快速获得权重及排名。
6. 通过 .htaccess 实现缓存策略
通过设置在浏览器上缓存静态文件可以提升网站的性能:
# year
“\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$”>
Header set Cache-Control “public”
Header set Expires “Thu, 15 Apr 2010 20:00:00 GMT”
Header unset Last-Modified
#2 hours
“\.(html|htm|xml|txt|xsl)$”>
Header set Cache-Control “max-age=7200, must-revalidate”
“\.(js|css)$”>
SetOutputFilter DEFLATE
Header set Expires “Thu, 15 Apr 2010 20:00:00 GMT”
7. 使用 GZIP 对输出进行压缩
在 .htaccess 中添加下面的代码可以将所有的 css、js 和 html 使用 GZIP 算法压缩:
mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
使用上面代码的前提是启用 mod_gzip 模块,你可以使用下面脚本来判断 Web 服务器是否提供 mod_deflate 支持:
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \
\.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary
如果 Web 服务器不支持 mod_deflate ,那么可使用下面方法:
“\.(txt|html|htm|php)”>
php_value output_handler ob_gzhandler
8. 强制要求使用 HTTPS 访问
通过以下脚本可以强制整个网站必须使用 https 方式访问:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
9. URL 重写
例如要将 zhibushi.php?id=12 重写为 zhibushi-12.html
RewriteEngine on
RewriteRule ^zhibushi-([0-9]+)\.html$ zhibushi.php?id=$1
将 zhibushi.php?id=12 重写为 zhibushi/ipod-nano/12.html
RewriteEngine on
RewriteRule ^zhibushi/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ zhibushi.php?id=$2
重定向没有 www 到有 www 的 URL 地址:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^viralpatel\.net$
RewriteRule (.*) http://www.zhibushi.net/$1 [R=301,L]
重写 zhibushi.com/user.php?username=xyz 到 zhibushi.com/xyz
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
重定向某个域名到一个 public_html 里新的子文件夹:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^zhibushi\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.zhibushi\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1
10. 阻止列出目录文件
使用下面代码可以防止列表目录里的所有文件:
Options -Indexes
或者
IndexIgnore *
11. 添加新的 MIME-Types
MIME-types 依赖于文件的扩展名,未能被识别的文件扩展名会当成文本数据传输
AddType application/x-endnote-connection enz
AddType application/x-endnote-filter enf
AddType application/x-spss-savefile sav
12. 防盗链
你不希望别人网站引用你站内的图片、css 等静态文件,也就是传说中的防盗链,可以使用如下脚本:
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{REQUEST_URI} !^/(wp-login.php|wp-admin/|wp-content/plugins/|wp-includes/).* [NC]
RewriteCond %{HTTP_REFERER} !^http://www.askapache.com.*$ [NC]
RewriteRule \.(ico|pdf|flv|jpg|jpeg|mp3|mpg|mp4|mov|wav|wmv|png|gif|swf|css|js)$ – [F,NS,L]
13. 指定上传文件的大小限制,适用于 PHP
php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 200
php_value max_input_time 200
上述脚本中,通过四个参数来设置上传文件的限制,第一个参数是文件的大小,第二个是 POST 数据的大小,第三个是传输的时间(单位秒),最后一个是解析上传数据最多花费的时间(单位秒)
14. 禁止脚本执行
Options -ExecCGI
AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi
15. 修改字符集和语言头
AddDefaultCharset UTF-8
DefaultLanguage en-US
16. 设置服务器时区(GMT)
SetEnv TZ America/Indianapolis
17. 强制 “File Save As” 提示
AddType application/octet-stream .avi .mpg .mov .pdf .xls .mp4
18. 保护单个文件
正常情况下 .htaccess 可用于限制整个目录的访问,但也可以只限制某个文件:
quiz.html>
order deny,allow
deny from all
AuthType Basic
AuthName “Characterology Student Authcate”
AuthLDAP on
AuthLDAPServer ldap://directory.characterology.com/
AuthLDAPBase “ou=Student, o=Characterology University, c=au”
require valid-user
satisfy any
19. 设置 Cookie
通过环境变量来设置 Cookie
Header set Set-Cookie “language=%{lang}e; path=/;” env=lang
基于请求设置 Cookie,该代码发送 Set-Cookie 头用于设置 Cookie 值为第二个括号里的匹配项
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)(de|es|fr|it|ja|ru|en)/$ – [co=lang:$2:.yourserver.com:7200:/]
20. 设置自定义的响应 Headers
Header set P3P “policyref=\”http://www.zhibushi.com/ewc/pp2.xml\””
Header set X-Pingback “http://www.zhibushi.com/xmwlrpc.php”
Header set Content-Language “en-US”
Header set Vary “Accept-Encoding”
21. 根据 User-Agent 来阻止请求
SetEnvIfNoCase ^User-Agent$ .*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider|leacher|collector|grabber|webpictures) HTTP_SAFE_BADBOT
SetEnvIfNoCase ^User-Agent$ .*(libwww-perl|aesop_com_spiderman) HTTP_SAFE_BADBOT
Deny from env=HTTP_SAFE_BADBOT
注意:在编辑.htaccess文件时,一定要使用代码编辑器进行编辑。
注意:在编辑.htaccess文件时,一定要使用代码编辑器进行编辑。
注意:在编辑.htaccess文件时,一定要使用代码编辑器进行编辑。
(重要的事说三遍)
如果你觉得以上方式太难看不懂,或觉得修改太麻烦。
http://tools.jb51.net/aideddesign/mkhtaccess
可以通过站长工具配置生成htaccess文件,或者自己也可以找到些桌面工具。