首页 文章

拒绝所有,只允许一个IP通过htaccess

提问于
浏览
143

我试图拒绝所有,只允许一个IP . 但是,我想让以下htaccess为该单个IP工作 . 我找不到兼顾两者的方法:拒绝所有并且只允许一个,加上以下选项:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

有没有办法让这项工作?

10 回答

  • 37
    order deny,allow
    deny from all
    allow from <your ip>
    
  • 7

    我知道这个问题已经有了接受的答案,但Apache documentation说:

    mod_access_compat提供的Allow,Deny和Order指令已弃用,将在以后的版本中消失 . 您应该避免使用它们,并避免过时的教程建议使用它们 .

    因此,更具前瞻性的答案是:

    <RequireAll>
        Require ip xx.xx.xx.xx yy.yy.yy.yy
    </RequireAll>
    

    希望,我帮助阻止此页面成为那些“过时的教程”之一 . :)

  • 29

    这可以通过使用为该任务设计的指令来改进 .

    ErrorDocument 403 /specific_page.html
    Order Allow,Deny
    Allow from 111.222.333.444
    

    其中111.222.333.444是您的静态IP地址 .

    使用“Order Allow,Deny”指令时,请求必须与Allow或Deny匹配,如果两者都不满足,则请求被拒绝 .

    http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#order

  • 330

    上面的略微修改版本,包括一个自定义页面,显示给那些被拒绝访问的人:

    ErrorDocument 403 /specific_page.html
    order deny,allow
    deny from all
    allow from 111.222.333.444
    

    ......那些不是来自111.222.333.444的请求会看到specific_page.html

    (发布此评论看起来很糟糕,因为新线路丢失)

  • 1

    通过改进以前的答案,可以在对站点进行更改时向用户显示维护页面:

    ErrorDocument 403 /maintenance.html
    Order Allow,Deny
    Allow from #.#.#.#
    

    哪里:

  • 88

    您可以在htaccess中使用以下内容来允许和拒绝访问您的站点:

    SetEnvIf remote_addr ^1\.2\3\.4\.5$ allowedip=1
    
    Order deny,allow
    deny from all
    allow from env=allowedip
    

    如果客户端IP地址与模式匹配,我们首先设置一个env变量 allowedip ,如果模式匹配,则env变量 allowedip 被赋值为 1 .

    在下一步中,我们使用Allow,deny指令来允许和拒绝对站点的访问 . Order deny,allow 表示 denyallow 的顺序 . deny from all 这一行告诉服务器拒绝所有人 . 最后一行 allow from env=allowedip 允许访问我们为其设置env变量的单个IP地址 .

    用您允许的IP地址替换 1\.2\.3\.4\.5 .

    参考:

  • 2

    我无法使用403方法,因为我想在我的服务器上的子文件夹中维护页面和页面图像,因此使用以下方法重定向到每个人的“维护页面”,但只有一个IP *

    RewriteEngine on
    RewriteCond %{REMOTE_ADDR} !**.**.**.*
    RewriteRule !^maintenance/ http://www.website.co.uk/maintenance/ [R=302,L]
    

    资料来源:Creating a holding page to hide your WordPress blog

  • 0

    您可以拥有多个IP甚至其他类型的允许,例如用户,主机名,...更多信息,请点击此处https://www.askapache.com/htaccess/setenvif/

    SetEnvIf remote_addr ^123.123.123.1$ allowedip=1
    SetEnvIf remote_addr ^123.123.123.2$ allowedip=1
    SetEnvIf remote_addr ^123.123.123.3$ allowedip=1
    SetEnvIf remote_addr ^123.123.123.4$ allowedip=1
    
    Order deny,allow
    deny from all
    allow from env=allowedip
    
  • 1

    除了@David Brown的回答之外,如果你想阻止IP,你必须首先允许所有IP然后阻止IP:

    <RequireAll>
          Require all granted
          Require not ip 10.0.0.0/255.0.0.0
          Require not ip 172.16.0.0/12
          Require not ip 192.168
        </RequireAll>
    
    First line allows all
    Second line blocks from 10.0.0.0 to 10.255.255.255
    Third line blocks from 172.16.0.0 to 172.31.255.255
    Fourth line blocks from 192.168.0.0 to 192.168.255.255
    

    您可以使用上述任何符号来满足您的CIDR需求 .

  • 1

    如果要使用mod_rewrite进行访问控制,可以使用用户代理,http引用,远程地址等条件 .

    RewriteCond %{REMOTE_ADDR} !=*.*.*.* #you ip address
    RewriteRule ^$ - [F]
    

    Refrences:

相关问题