I need .htaccess code that will:

Restrict a specific IP from a specific page.
Allow that IP on any other pages but the restricted one.

I already tried this and it does not work:

<files submit.php>
order allow,deny
deny from 1.2.3.
deny from 1.2.
allow from all
</files> 

Does anyone know a better way, maybe using mod_rewrite?

Dani AI

Generated

resolved this by rebuilding Apache, which is a common root cause: access directives depend on which modules are present and which Apache version you run. For readers who land on this thread later, the safest approach is to use the modern Apache 2.4 authorization syntax when possible, and fall back to a mod_rewrite rule if you do not control the server build.

Example (Apache 2.4, preferred): use a <Files> block with the Require containers to allow everyone except a single IP.

<Files "submit.php">
  <RequireAll>
    Require all granted
    Require not ip 1.2.3.4
  </RequireAll>
</Files>

See the Files and Require documentation for details: Files directive docs and Require (mod_authz_core).

Fallback (mod_rewrite): if you cannot rely on authz modules, block the request with rewrite rules.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REMOTE_ADDR} ^1\.2\.3\.4$
  RewriteRule ^submit\.php$ - [F,L]
</IfModule>

See the mod_rewrite docs: mod_rewrite.

Troubleshooting tips: confirm required modules are loaded with apachectl -M or httpd -M; check error logs (e.g., /var/log/apache2/error.log or /var/log/httpd/error_log) after reloading; and be aware of reverse proxies or CDNs — REMOTE_ADDR may be the proxy IP, in which case use mod_remoteip or block at the proxy/firewall. was correct to point out host-specific behavior — if the host forbids custom module changes, use the rewrite approach or provider-side firewall rules.

Recommended Answers

All 2 Replies

I thought of using mod_rewrite is an alternative if 'deny from' wasn't working. It turns out I had a bad Apache build. When I rebuilt Apache, the 'deny from' code I posted in the first place ended up working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.