Radley Sustaire
Radley Sustaire

Reputation: 3399

.htaccess rules by IP address

I'm looking for a way to enable some .htaccess rules on an IP or hostname basis (hopefully with regex). This is all over stackoverflow, but I can't find any that are generic rules that apply to a block of rules (And in fact, that may not be how .htaccess works. I'm not sure!).

I do NOT need a rewrite rule. I do NOT need access/deny flags. I just want to change some PHP flags and file behavior for some development IP addresses on a few of our servers.

For example, to enable debug mode /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>

Note that I do not need a PHP solution for PHP debugging, this is just an example.

Upvotes: 1

Views: 1474

Answers (1)

anubhava
anubhava

Reputation: 786241

You will usually find many comments/posts saying this is not possible. That would have been possible easily in newer Apache versions (2.4 and newer).

However on older Apache 2.2* This is something I have used as a workaround solution

  1. Create a symbolic link of /index.php and name it something like /myinde.php using this command:

    ln -s index.php myindex.php
    
  2. Add this code in your 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 on top forwards all the requests from your IP address to /myindex.php making original URI available as PATH_INFO.

Then block of code inside <Files myindex.php> is executed for the file myindex.php.

Upvotes: 1

Related Questions