【问题标题】:routing in laravel with multiple htaccess在具有多个 htaccess 的 laravel 中路由
【发布时间】:2018-10-14 17:42:31
【问题描述】:

我想要特定路线的特定 htaccess 文件。

例如,我想允许所有 ips 访问我网站的主路由,但阻止特定 ips 访问仅支付路由或特定路由。

请帮我解决这个问题。

【问题讨论】:

    标签: laravel .htaccess routes


    【解决方案1】:

    你可以使用三种解决方案:

    1. laravelmiddleware

    这里:

    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

    这里:

    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?

    【讨论】:

      【解决方案2】:

      您应该使用middleware 进行访问控制,.htaccess“可以”在技术上用于此目的,但强烈建议不要这样做。

      基于ip过滤的可能逻辑粗略示例,可以放在新创建的中间件的句柄函数中:

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

      【讨论】:

        猜你喜欢
        • 2019-03-07
        • 1970-01-01
        • 2016-03-04
        • 2016-10-12
        • 2021-05-09
        • 2014-05-04
        • 2011-09-07
        • 2018-08-24
        • 2015-04-23
        相关资源
        最近更新 更多