How to block access to a particular route in .htac

2019-09-19 10:59发布

问题:

I need to block access to a particular route in my web application using a .htaccess file for everyone except a list of IP's. When I say block and whitelist IP's I want to use the following on particular route

order deny,allow
deny from all
allow from 1.1.1.1
allow from 2.2.2.2

I tried using the Location directive, but it is not allowed in .htaccess.

I do not have access to the server config file since it is a managed hosting provider

The route I want to block is for eg: http://www.example.com/route1

Is there a way?

Thanks for the help in advance

回答1:

You can definitely achieve this using multiple methods.

.htaccess files:

<files route1>
    order deny,allow
    deny from all
    allow from my.ip.address
</files>


回答2:

If you are looking at whitelisting multiple ip's I would suggest the follow method:

<Files myfile.php>
  order deny,allow
  deny from all
  allow from env=allowip

  #Office 1
  #132.11.32.222
  SetEnvIf X-FORWARDED-FOR "^132\.11\.32\.222" allowip

  #Office 2
  #142.11.32.222
  SetEnvIf X-FORWARDED-FOR "^142\.11\.32\.222" allowip
</Files>