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

pun*_*bit 148 .htaccess

我试图拒绝所有,只允许一个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>
Run Code Online (Sandbox Code Playgroud)

有没有办法让这项工作?

小智 342

order deny,allow
deny from all
allow from <your ip> 
Run Code Online (Sandbox Code Playgroud)

  • 注意!Apache对htaccess中的空间很敏感.拒绝,允许之间不允许任何空格.即不写命令拒绝,允许. (88认同)
  • [关于2.4文档的注释](https://httpd.apache.org/docs/2.4/howto/access.html):_ mod_access_compat提供的Allow,Deny和Order指令已弃用,将来会消失版.您应该避免使用它们,并避免过时的教程建议使用它们._ (9认同)
  • 正如@ MA-Maddin指出的那样, - 然后不推荐使用mod_access_compat.[这篇文章](/sf/ask/2650596091/)显示了如何在较新版本中进行操作.如果服务器正在使用代理,那么也应该考虑,因为这将显示访问者的代理IP.[这篇文章](/sf/ask/2054933121/)提出了解决方案. (3认同)
  • 信息: - 它也适用于IPv6!只需添加另一行`from your your your yourvv6` (2认同)

Rok*_*gar 105

我知道这个问题已经有了一个公认的答案,但是Apache文档说:

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

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

<RequireAll>
    Require ip xx.xx.xx.xx yy.yy.yy.yy
</RequireAll>
Run Code Online (Sandbox Code Playgroud)

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

  • 干得好!这应该是可以接受的解决方案,因为它不仅有效而且经久耐用。 (2认同)
  • 这个答案的例子允许从两个IP访问吗?那么,要添加多个 IP,我们只需在它们之间添加一个空格即可?您能否将“需要 ip”放在多行上以使其更易于用户阅读,还是所有 IP 都必须位于一行上? (2认同)

小智 38

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

ErrorDocument 403 /specific_page.html
Order Allow,Deny
Allow from 111.222.333.444
Run Code Online (Sandbox Code Playgroud)

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

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

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


Oli*_*vic 31

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

ErrorDocument 403 /specific_page.html
order deny,allow
deny from all
allow from 111.222.333.444
Run Code Online (Sandbox Code Playgroud)

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

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


Iva*_*nRF 8

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

ErrorDocument 403 /maintenance.html
Order Allow,Deny
Allow from #.#.#.#
Run Code Online (Sandbox Code Playgroud)

哪里:


Ram*_*eed 6

在 .htaccess 文件中添加以下命令。并将该文件放入您的 htdocs 文件夹中。

Order Deny,Allow
Deny from all
Allow from <your ip> 
Allow from <another ip> 
Run Code Online (Sandbox Code Playgroud)


小智 5

除了@David Brown 的回答之外,如果你想阻止一个 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>
Run Code Online (Sandbox Code Playgroud)
  • 第一行允许所有
  • 第二行块从10.0.0.010.255.255.255
  • 第三行块从172.16.0.0172.31.255.255
  • 第四行块从192.168.0.0192.168.255.255

您可以使用上面提到的任何符号来满足您的 CIDR 需求。

  • 谢谢,这是处理 Apache 2.4 的正确方法! (2认同)