21非常有用的htaccess的方法与技巧
Apache Web服务器有一个伟大的方式来处理信息,使用htaccess的文件。htaccess文件(超文本的访问)的目录级别的配置文件,允许Web服务器配置的分散管理的默认名称。htaccess文件是放置在Web树,是能够覆盖服务器的全局配置的一个子集,这个子集的程度是由Web服务器管理员定义。。htaccess的最初目的是让每个目录访问控制(例如,需要密码才能访问的内容),因此这个名字。如今的。htaccess可以覆盖许多其他配置设置,主要涉及内容的控制,例如,内容类型和字符集,CGI处理程序,等等。
以下是一些非常有用的htaccess的技巧。 站长百科 PHP教程同步首发
1。自定义目录索引文件
- DirectoryIndex index.html index.php index.htm
您的htaccess文件中使用上面的片段,你可以改变默认的索引文件的目录。如果一个用户请求/ foo /中,Apache将服务是/ foo / index.html的,或您指定的任何文件。
2。自定义错误页面
- ErrorDocument 404 errors/404.html
您可能希望将用户重定向到一个错误页面是像404的HTTP错误发生。您可以使用htaccess文件中映射的404错误错误页errors/404.html的上面的片段。你也可能想要写一个共同的页面,所有HTTP错误如下:
- ErrorDocument 404 /psych/cgi-bin/error/error?404
3。控制在文件和目录级别的访问
htaccess是最常用的限制或拒绝个别文件和文件夹的访问。一个典型的例子是“包括”文件夹。您的网站的网页可以调用这些脚本,所有他们喜欢的,但你不希望用户访问这些文件,直接在网上。在这种情况下,你会下降。htaccess文件,像这样的内容包括夹。
- # no one gets in here!
- deny from all
这将拒绝所有直接访问该文件夹中的任何文件。你可以与你的情况更具体,例如访问限制到一个特定的IP范围,这里有一个方便的顶级本地测试服务器的规则。
- # no nasty crackers in here!
- order deny,allow
- deny from all
- allow from 192.168.0.0/24
- # this would do the same thing..
- #allow from 192.168.0
一般来说,这些要求的各种反弹防火墙无论如何,而是一个活生生的服务器上,他们成为有用过滤掉不受欢迎的IP块,已知的风险,很多事情。
有时候,你只会想禁止一个IP,也许是一些持久的机器人,不按规则玩。
- # someone else giving the ruskies a bad name..
- order allow,deny
- deny from 83.222.23.219
- allow from all
4。修改环境变量
环境变量包含服务器端所使用的信息,包括和CGI。设置/取消设置环境变量使用SETENV和UnSetEnv。
- SetEnv SITE_WEBMASTER "Jack Sprat"
- SetEnv SITE_WEBMASTER_URI mailto:[email protected]
- UnSetEnv REMOTE_ADDR
5。301重定向使用htaccess的
如果你想从旧的文件重定向到新:
- Redirect 301 /old/file.html http://yourdomain.com/new/file.html
使用以下重定向整个目录。
- RedirectMatch 301 /blog(.*) http://yourdomain.com/$1
6。实施计划。htaccess文件高速缓存
缓存的静态文件,并提高您的网站的性能。
- # 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 DEFLATE
- Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
- </FilesMatch>
7。使用GZIP压缩输出
下面的代码片断添加到您的htaccess文件和压缩所有的CSS,JS,用gzip压缩的HTML文件。
- <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>
上面的代码工作mod_gzip模块只有在您的Web服务器中启用。如果您的网络服务器提供支持mod_deflate模块,您可能需要添加下面的代码片断。
- <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>
如果您的服务器不支持mod_deflate模块,那么你可能需要使用下面的代码片断。
- <FilesMatch "\.(txt|html|htm|php)">
- php_value output_handler ob_gzhandler
- </FilesMatch>
8。浏览器重定向到HTTPS(SSL)
下面的代码片断添加到您的htaccess的和整个网站重定向到HTTPS。
- RewriteEngine On
- RewriteCond %{HTTPS} !on
- RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
9。重写的URL使用htacccess
重写product.php?ID = 12产品-12.html
- RewriteEngine on
- RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
的重写product.php?ID = 12至product/ipod-nano/12.html
- RewriteEngine on
- RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
非WWW网址重定向到www的网址
- RewriteEngine On
- RewriteCond %{HTTP_HOST} ^viralpatel\.net$
- RewriteRule (.*) http://www.viralpatel.net/$1 [R=301,L]
重写yoursite.com / user.php?用户名= XYZ yoursite.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} ^test\.com$ [OR]
- RewriteCond %{HTTP_HOST} ^www\.test\.com$
- RewriteCond %{REQUEST_URI} !^/new/
- RewriteRule (.*) /new/$1
10。防止目录列表
添加下面的代码片段,以避免目录列表。
- Options -Indexes
或
- IndexIgnore *
11。添加新的MIME类型
取决于文件扩展名的文件类型。无法识别的文件扩展名被视为文本数据,并下载损坏。
- 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。指定上传htaccess的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
在上面的。htaccess文件,上传能力由四个参数,第一个是最大上传文件大小的增加,第二个是后数据的最大大小,第三个是在几秒钟的时间上限,允许脚本运行之前,它是终止解析器允许一个脚本解析输入数据,如像文件上传,最后一个是在几秒钟内最大的一次,POST和GET数据。
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。力“的文件另存为”提示“
- AddType application/octet-stream .avi .mpg .mov .pdf .xls .mp4
18。一个单一的文件保护
通常情况下的。htaccess适用于整个目录。随着 指令,你可以限制到特定的文件:
- <Files 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
- </Files>
19。使用htaccess的设置cookie
设置环境变量的Cookie
- Header set Set-Cookie "language=%{lang}e; path=/;" env=lang
基于请求设置的Cookie。此代码将创建在客户端上的Cookie,在第二个括号匹配项的值的Set-Cookie头。
- RewriteEngine On
- RewriteBase /
- RewriteRule ^(.*)(de|es|fr|it|ja|ru|en)/$ - [co=lang:$2:.yourserver.com:7200:/]
20。寄自定义页眉
- 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头阻塞请求
- 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