0

I have a url path defined in rewrite rules ('/admin') which I would like to restrict access to only 1 ip address. I'd image the best way would to use .htaccess rather than coding it in. I've tried this but it didn't work. It blocked all urls, not just urls with /admin.

<Location /admin>
    Deny from all
    Allow from x.x.x.x
</Location>
1
  • Was my answer useful to solving your problem?
    – sa289
    Apr 21, 2015 at 20:12

2 Answers 2

0

Location directive is not allowed in .htaccess file. Otherwise, you can add these lines in your Apache configuration file / virtual host file.

0

You could use RewriteRule's [F] flag to deny the access to all but the permitted IP(s). See the following for an example (I didn't test this but I think it's correct). If you want to use the allow directive, Another way to do it is to use Rewrite directives to set an environment variable using the [E=...] flag of RewriteRule and then do Allow from env=environment_var_goes_here (see http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#allow)

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/admin.*
RewriteCond %{REMOTE_ADDR} !^x\.x\.x\.x$
RewriteRule .* - [F]

Note: I wrote that example to match any URL starting with /admin, but if you want to just match /admin, remove the .* and replace it with a $.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .