首页 > 解决方案 > 在具有多个 htaccess 的 laravel 中路由

问题描述

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

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

请帮我解决这个问题。

标签: laravel.htaccessroutes

解决方案


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

  1. laravel中间件

这里:

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 允许和拒绝 IP

  1. CloudFlare

如何控制对我网站的 IP 访问?


推荐阅读