日期:2014-05-16  浏览次数:21358 次

htaccess配置
问题:想让某个网站只能本地和指定的域名能访问。
解决办法:通过htaccess设置访问控制权限
apache的使用指南手册说优先使用LimitExcept,如下

A <LimitExcept> section should always be used in preference to a <Limit> section when restricting access, since a <LimitExcept> section provides protection against arbitrary methods.


看了很久的网上资料,发现那些设置IP地址和域名地址的方法都不起作用。设置了allow也无法访问

例如allow from *.taobao.com Allow from 192.168.2.3
都没用的。但是通过Env环境变量来判断是有效的。
SetEnvIfNoCase Referer "^http://mydomain.com$" locally_linked


然后  allow from env=locally_linked
SetEnvIfNoCase 和SetEnvIf的区别是前者不区分大小写。后者区分。

那么,限制某个域名可以通过referer来控制。但是本地的请求头中没有referer属性,那怎么办。

就要理解到SetEnvIf的应用规则了。SetEnvIf是按照正则表达式的方式来判断的,那写个referer是空的正则表达式就可以了吧。
SetEnvIf Referer ^$ locally_linked


OK。测试顺利通过。

但是,如果是图片和JS,css请求又有问题了。因为这些请求是带referer的。
那很简单了,只要添加上该系统域名就可以了

最后是这样的:

  SetEnvIf Referer ^http://test.login.nmg.com.hk/ locally_linked
   SetEnvIf Referer ^$ locally_linked
   SetEnvIf Referer ^http://test.authserver.nmg.com.hk locally_linked
   <Limit GET POST HEAD>
	 order Deny,allow
	 Deny from all
	 allow from env=locally_linked 
   </Limit>
   
   <Limit PUT DELETE>
	 Deny from all
   </Limit>


^表示字符串开始
$表示字符串结束

SetEnvIfNoCase Referer "^http://www.mydomain.com/" locally_linked=1
SetEnvIfNoCase Referer "^http://www.mydomain.com$" locally_linked=1
SetEnvIfNoCase Referer "^http://mydomain.com/" locally_linked=1
SetEnvIfNoCase Referer "^http://mydomain.com$" locally_linked=1
SetEnvIfNoCase Referer "^$" locally_linked=1

<FilesMatch "\.(gif|php|html|php3|htm|jpe?g)$">
  Order Allow,Deny
  Allow from env=locally_linked
</FilesMatch>

限制用户代码类型访问权限
SetEnvIf User-Agent ^KnockKnock/2\.0 let_me_in
Order Deny,Allow
Deny from all
Allow from env=let_me_in

_______________________________________________________________________

页面跳转

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


跳转保存get参数
RwriteRule ^(.*)$ index.php?param=$1 [QSA,L]

参考:
http://forums.powweb.com/showthread.php?t=73248.

apache htaccess文件资料:

http://www.phpchina.com/resource/manual/apache/mod/core.html#accessfilename