1

I want specific htaccess file for specific route.

For example, i want to allow all ips to home route of my site but prevent access specific ips to just payment route or specific routes.

Please help me with this problem.

2 Answers 2

1

you can use three solution:

  1. laravel middleware

here:

namespace App\Http\Middleware;

use Closure;

use Symfony\Component\HttpFoundation\IpUtils;

class RedirectInvalidIPs
{

    protected $ips = [
        '65.202.143.122',
        '148.185.163.203'
    ];

    protected $ipRanges = [
        '10.11.3.1',
    ];

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        foreach ($request->getClientIps() as $ip) {
            if (! $this->isValidIp($ip) && ! $this->isValidIpRange($ip)) {
                return redirect('/');
            }
        }

        return $next($request);
    }

    protected function isValidIp($ip)
    {
        return in_array($ip, $this->ips);
    }

    protected function isValidIpRange($ip)
    {
        return IpUtils::checkIp($ip, $this->ipRanges);
    }
}
  1. .htaccess

here:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} /thisdirectoryandallcontents
RewriteCond %{REMOTE_ADDR} !=111.111.111.111
RewriteRule ^.*$ /maintenance.php [R=302,L]

Nginx allow and deny IP

  1. CloudFlare

How do I control IP access to my site?

Sign up to request clarification or add additional context in comments.

Comments

0

You should use middleware for access control, the .htaccess 'can' technically be used for this, but it is highly recommended not to.

Rough example of possible logic to filter based on ip, that can be put in the handle function of newly created middleware:

    if (!in_array($request->ip, ['127.0.0.1', '::1'])) {
        abort(403);
    }

    return $next($request);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.