1. 定制目录的 Index 文件
|
1 |
DirectoryIndex index.html index.php index.htm |
2. 自定义错误页
|
1 |
ErrorDocument 404 errors/404.html |
|
1 |
ErrorDocument 404 /psych/cgi-bin/error/error?404 |
3. 控制访问文件和目录的级别
.htaccess 经常用来限制和拒绝访问某个文件和目录,例如我们有一个 includes 文件夹,这里存放一些脚本,我们不希望用户直接访问这个文件夹,那么通过下面的脚本可以实现:|
1
2 |
# no one gets in here!deny from all |
|
1
2
3
4
5
6 |
# no nasty crackers in here!order deny,allowdeny from allallow from 192.168.0.0/24# this would do the same thing..#allow from 192.168.0 |
|
1
2
3
4 |
# someone else giving the ruskies a bad name..order allow,denydeny from 83.222.23.219allow from all |
4. 修改环境变量
环境变量包含了服务器端 CGI 的一些扩展信息,可使用 SetEnv 和 UnSetEnv 进行设置以及取消设置.|
1
2
3
4 |
SetEnv SITE_WEBMASTER "Jack Sprat"SetEnv SITE_WEBMASTER_URI mailto:Jack.Sprat@characterology.com UnSetEnv REMOTE_ADDR |
5. 301 重定向
如果你希望某个页面跳转到新的页面:|
1 |
Redirect 301 /old/file.html http://yourdomain.com/new/file.html |
|
1 |
RedirectMatch 301 /blog(.*) http://yourdomain.com/$1 |
6. 通过 .htaccess 实现缓存策略
通过设置在浏览器上缓存静态文件可以提升网站的性能:|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
# year<FilesMatch ".(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</FilesMatch>#2 hours<FilesMatch ".(html|htm|xml|txt|xsl)$">Header set Cache-Control "max-age=7200, must-revalidate"</FilesMatch><FilesMatch ".(js|css)$">SetOutputFilter DEFLATEHeader set Expires "Thu, 15 Apr 2010 20:00:00 GMT"</FilesMatch> |
7. 使用 GZIP 对输出进行压缩
在 .htaccess 中添加下面的代码可以将所有的 css、js 和 html 使用 GZIP 算法压缩:|
1
2
3
4
5
6
7
8
9
10 |
<IfModule 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.*</IfModule> |
|
1
2
3
4
5
6
7 |
<Location> 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</Location> |
|
1
2
3 |
<FilesMatch ".(txt|html|htm|php)"> php_value output_handler ob_gzhandler</FilesMatch> |
8. 强制要求使用 HTTPS 访问
通过以下脚本可以强制整个网站必须使用 https 方式访问:|
1
2
3 |
9. URL 重写
例如要将 product.php?id=12 重写为 product-12.html|
1
2 |
RewriteEngine onRewriteRule ^product-([0-9]+).html$ product.php?id=$1 |
|
1
2 |
RewriteEngine onRewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+).html$ product.php?id=$2 |
|
1
2
3 |
RewriteEngine OnRewriteCond %{HTTP_HOST} ^viralpatel.net$RewriteRule (.*) http://www.viralpatel.net/$1 [R=301,L] |
|
1
2
3 |
RewriteEngine OnRewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1 |
|
1
2
3
4
5 |
RewriteEngine OnRewriteCond %{HTTP_HOST} ^test.com$ [OR]RewriteCond %{HTTP_HOST} ^www.test.com$RewriteCond %{REQUEST_URI} !^/new/RewriteRule (.*) /new/$1 |
10. 阻止列出目录文件
使用下面代码可以防止列表目录里的所有文件:|
1 |
Options -Indexes |
|
1 |
IndexIgnore * |
11. 添加新的 MIME-Types
MIME-types 依赖于文件的扩展名,未能被识别的文件扩展名会当成文本数据传输|
1
2
3 |
AddType application/x-endnote-connection enzAddType application/x-endnote-filter enfAddType application/x-spss-savefile sav |
12. 防盗链
你不希望别人网站引用你站内的图片、css 等静态文件,也就是传说中的防盗链,可以使用如下脚本:|
1
2
3
4 |
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
|
1
2
3
4 |
php_value upload_max_filesize 20Mphp_value post_max_size 20Mphp_value max_execution_time 200php_value max_input_time 200 |
14. 禁止脚本执行
|
1
2 |
Options -ExecCGIAddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi |
15. 修改字符集和语言头
|
1
2 |
AddDefaultCharset UTF-8DefaultLanguage en-US |
16. 设置服务器时区(GMT)
|
1 |
SetEnv TZ America/Indianapolis |
17. 强制 “File Save As” 提示
|
1 |
AddType application/octet-stream .avi .mpg .mov .pdf .xls .mp4 |
18. 保护单个文件
正常情况下 .htaccess 可用于限制整个目录的访问,但也可以只限制某个文件:|
1
2
3
4
5
6
7
8
9
10
11 |
<Files quiz.html>order deny,allowdeny from allAuthType BasicAuthName "Characterology Student Authcate"AuthLDAP onAuthLDAPServer ldap://directory.characterology.com/AuthLDAPBase "ou=Student, o=Characterology University, c=au"require valid-usersatisfy any</Files> |
19. 设置 Cookie
通过环境变量来设置 Cookie|
1 |
Header set Set-Cookie "language=%{lang}e; path=/;" env=lang |
|
1
2
3 |
RewriteEngine OnRewriteBase /RewriteRule ^(.*)(de|es|fr|it|ja|ru|en)/$ - [co=lang:$2:.yourserver.com:7200:/] |
20. 设置自定义的响应 Headers
|
1
2
3
4 |
Header set P3P "policyref="http://www.askapache.com/w3c/p3p.xml""Header set X-Pingback "http://www.askapache.com/xmlrpc.php"Header set Content-Language "en-US"Header set Vary "Accept-Encoding" |
21. 根据 User-Agent 来阻止请求
|
1
2
3 |
SetEnvIfNoCase ^User-Agent$ .*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider|leacher|collector|grabber|webpictures) HTTP_SAFE_BADBOTSetEnvIfNoCase ^User-Agent$ .*(libwww-perl|aesop_com_spiderman) HTTP_SAFE_BADBOTDeny from env=HTTP_SAFE_BADBOT |