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

[NC] nocase|NC'(忽略大小写)
[P] proxy
[L] last
H|handler
N|next
R|redirect
3.Apache mod_rewrite规则重写的标志一览

    1) R[=code](force redirect) 强制外部重定向
    强制在替代字符串加上http://thishost[:thisport]/前缀重定向到外部的URL.如果code不指定,将用缺省的302 HTTP状态码。
    2) F(force URL to be forbidden)禁用URL,返回403HTTP状态码。
    3) G(force URL to be gone) 强制URL为GONE,返回410HTTP状态码。
    4) P(force proxy) 强制使用代理转发。
    5) L(last rule) 表明当前规则是最后一条规则,停止分析以后规则的重写。
    6) N(next round) 重新从第一条规则开始运行重写过程。
    7) C(chained with next rule) 与下一条规则关联

    如果规则匹配则正常处理,该标志无效,如果不匹配,那么下面所有关联的规则都跳过。

    T=MIME-type(force MIME type) 强制MIME类型
    9) NS (used only if no internal sub-request) 只用于不是内部子请求
    10) NC(no case) 不区分大小写
    11) QSA(query string append) 追加请求字符串
    12) NE(no URI escaping of output) 不在输出转义特殊字符
    例如:RewriteRule /foo/(.*) /bar?arg=P1%3d$1 [R,NE] 将能正确的将/foo/zoo转换成/bar?arg=P1=zoo
    13) PT(pass through to next handler) 传递给下一个处理
    例如:
    RewriteRule ^/abc(.*) /def$1 [PT] # 将会交给/def规则处理
    Alias /def /ghi
    14) S=num(skip next rule(s)) 跳过num条规则
    15) E=VAR:VAL(set environment variable) 设置环境变量

APACHE FLAGS 资料
http://httpd.apache.org/docs/current/rewrite/flags.html

猜你喜欢

转载自lhdst-163-com.iteye.com/blog/1924830