【问题标题】:.htaccess rules by IP address.htaccess 规则(按 IP 地址)
【发布时间】:2014-02-05 21:39:05
【问题描述】:

我正在寻找一种基于 IP 或主机名启用某些 .htaccess 规则的方法(希望使用正则表达式)。这一切都在stackoverflow上,但我找不到任何适用于规则块的通用规则(事实上,这可能不是.htaccess的工作方式。我不确定!)。

我不需要重写规则。我不需要访问/拒绝标志。我只是想为我们的一些服务器上的一些开发 IP 地址更改一些 PHP 标志和文件行为。

例如,启用调试模式 /error_log/

<magical_ip_filter_tag "123.456.78.901">
  php_flag display_startup_errors on
  php_flag display_errors on
  php_flag html_errors on
  php_flag log_errors on
  php_value error_log  /home/path/public_html/domain/PHP_errors.log
</magical_ip_filter_tag>

请注意,我不需要 PHP 解决方案来进行 PHP 调试,这只是一个示例。

【问题讨论】:

  • 你的 Apache 版本是多少?
  • Apache v2.2.22,@JimLewis 我无法通过搜索找到这样的问题,谢谢。虽然那里的答案不是我特别希望的,但我想实际上可能没有答案。

标签: php .htaccess access-control


【解决方案1】:

你通常会发现很多 cmets/posts 说这是不可能的。这在较新的 Apache 版本(2.4 和更新版本)中很容易实现。

但是在较旧的 Apache 2.2* 上,这是我用作解决方法

的东西
  1. 使用以下命令创建/index.php 的符号链接并将其命名为/myinde.php

    ln -s index.php myindex.php
    
  2. 将此代码添加到您的DocumentRoot/.htaccess:

    RewriteCond %{REMOTE_ADDR} ^123\.456\.78\.901$
    RewriteRule !^myindex.php/ myindex.php%{REQUEST_URI} [L]
    
    <Files myindex.php>
      php_flag display_startup_errors on
      php_flag display_errors on
      php_flag html_errors on
      php_flag log_errors on
      php_value error_log  /home/path/public_html/domain/PHP_errors.log
    </Files>
    

RewriteRule 顶部将所有请求从您的 IP 地址转发到 /myindex.php,使原始 URI 可用作 PATH_INFO

然后为文件myindex.php 执行&lt;Files myindex.php&gt; 内的代码块。

【讨论】:

  • 您是指ln -s 而不是ls -n 来创建符号链接吗?我确实在这里看到了解决方法,将用户带到普通用户无法访问的页面,然后检查他们是否正在访问该页面。但是,这会取代普通 index.php 的功能吗?还是我应该从 myindex.php 中重新包含标准 index.php?
  • 抱歉打错字了,是的,我的意思是ln -s。不,您不需要包含index.php,因为myindex.php 只是index.php 的符号链接
  • 哦,我明白了!我不清楚什么是符号链接。 “符号链接是一种特殊类型的文件,其中包含对另一个文件的引用”。听起来这将是一个足够好的解决方法。太糟糕了,它需要一个服务器端命令,但至少对我来说,这不是问题。谢谢!
  • 不客气,很高兴它成功了。我自己在几个网站上使用了这个技巧。
猜你喜欢
  • 1970-01-01
  • 2017-05-05
  • 2011-06-01
  • 2013-02-15
  • 2016-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-11
相关资源
最近更新 更多