温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Apache配置访问控制、禁用php解析、rewrite伪静态、限定user_agent

发布时间:2020-07-10 07:42:17 来源:网络 阅读:3194 作者:模范生 栏目:web开发

访问控制

以下针对apache虚拟主机配置

网站访问,限制指定ip才可以访问。只允许内网登录,不允许其他地址登录。

Order定义顺序,先deny,然后执行下面的deny from all,然后执行allow,再执行allow from 127.0.0.1,意思是拒绝所有的,只允许本地可以访问。

<Directory /data/www/>

<filesmatch ".*">

     Order deny,allow

     Deny from all

     Allow from 127.0.0.1

</filesmatch>

</Directory>


针对请求的url去限制,只允许内网和指定ip可以访问包含admin关键词的地址,一般为管理后台;

<filesmatch "(.*)admin(.*)">

          Order deny,allow

          Deny from all

          Allow from 127.0.0.1

          Allow from 2.2.2.2

</filesmatch>

假如该虚拟机的域名为 domain.com , 这样配置后,除了 127.0.0.1 和 2.2.2.2 外,其他ip访问以下类似的uri时都会直接禁止的。 

http://domain.com/1212admin.txt

http://domain.com/admin.php

http://domain.com/1212/admin.html 

Apache配置访问控制、禁用php解析、rewrite伪静态、限定user_agentApache配置访问控制、禁用php解析、rewrite伪静态、限定user_agent


某个目录下禁止解析php,目录可以自定义;php解析失败的话会显示源代码,对于网站来说不安全。

<Directory /data/www/>

    php_admin_flag engine off            

    <filesmatch "(.*)php">

         Order deny,allow

         Deny from all

    </filesmatch>

</Directory>

[root@localhost www]# curl -x127.0.0.1:80 www.111.com/forum.php -I
HTTP/1.1 403 Forbidden
Date: Mon, 04 May 2015 07:41:15 GMT
Server: Apache/2.2.29 (Unix) DAV/2 PHP/5.3.28
Content-Type: text/html; charset=iso-8859-1

Apache配置访问控制、禁用php解析、rewrite伪静态、限定user_agentApache配置访问控制、禁用php解析、rewrite伪静态、限定user_agent



apache rewrite模块应用——伪静态网页

mod_rewrite是Apache的一个非常强大的功能,它可以实现伪静态页面。

伪静态页面,网址域名后面的地址规范填写,看起来更美观,适合SEO搜索。

discuz! 管理后台页面——全局——左侧的SEO设置——URL伪静态,右边的可用选项全部打勾。

Apache配置访问控制、禁用php解析、rewrite伪静态、限定user_agent

discuz伪静态配置,写入到虚拟主机配置rewrite模块中;forum是版块,thread是帖子;

 <IfModule mod_rewrite.c>

RewriteEngine on

RewriteRule ^/topic-(.+)\.html$ /portal.php?mod=topic&topic=$1&%1

RewriteRule ^/article-([0-9]+)-([0-9]+)\.html$ /portal.php?mod=view&aid=$1&page=$2&%1

RewriteRule ^/forum-(\w+)-([0-9]+)\.html$ /forum.php?mod=forumdisplay&fid=$1&page=$2&%1

RewriteRule ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1

RewriteRule ^/group-([0-9]+)-([0-9]+)\.html$ /forum.php?mod=group&fid=$1&page=$2&%1

RewriteRule ^/space-(username|uid)-(.+)\.html$ /home.php?mod=space&$1=$2&%1

RewriteRule ^/blog-([0-9]+)-([0-9]+)\.html$ /home.php?mod=space&uid=$1&do=blog&id=$2&%1

RewriteRule ^/archiver/(fid|tid)-([0-9]+)\.html$ /archiver/index.php?action=$1&value=$2&%1

RewriteRule ^/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ /plugin.php?id=$1:$2&%1

</IfModule>


实验结果:

帖子地址为:http://www.111.com/forum.php?mod=viewthread&tid=1&extra=

伪静态之后为:http://www.111.com/thread-1-1-1.html


apache 限制指定user_agent

有些user_agent 不是我们想要的,可以通过rewrite功能针对 %{HTTP_USER_AGENT} 来rewirete到403页,从而达到限制某些user_agent的请求。

apache的rewrite功能有一项就是forbidden,那就是 F,重写规则会显示403错误,无法打开网页。


限制curl和IE浏览器8.0不能访问,360的浏览器版本为IE 7.0可以访问。USER_AGENT具体的版本需要看日志的详细信息,然后进行限制。

例如:("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)")

如禁用所有的IE浏览器:RewriteCond %{HTTP_USER_AGENT}  ^.*MSIE*


<IFModule mod_rewrite.c>

        RewriteEngine on

        RewriteCond %{HTTP_USER_AGENT}  ^.*MSIE\ 8.0* [NC,OR] 

        RewriteCond %{HTTP_USER_AGENT}  ^.*curl* [NC]

        RewriteRule  .*  -  [F]

</IFModule>


测试结果,使用curl本地解析网站为403错误;网页访问IE提示403错误,360浏览器可以打开。

[root@localhost www]# curl -x127.0.0.1:80 www.1111.com -I
HTTP/1.1 403 Forbidden
Date: Thu, 07 May 2015 02:09:58 GMT
Server: Apache/2.2.29 (Unix) DAV/2 PHP/5.3.28
Content-Type: text/html; charset=iso-8859-1

Apache配置访问控制、禁用php解析、rewrite伪静态、限定user_agent



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI