在Laravel 5中如何允许和拒绝.htacces文件?

时间:2018-08-10 05:10:01

标签: laravel

这是我的.htacces文件

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>
Options +FollowSymLinks
RewriteEngine On
# THIS IS NEW
#RewriteBase /

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# THIS IS NEW
#RewriteRule ^index\.php$ - [L]
#RewriteRule . /index.php [L]

我只想允许几个IP可以访问我的网站,除了htacces之外还有其他使用方式,我是Laravel的新手,请帮忙,谢谢

2 个答案:

答案 0 :(得分:0)

使用中间件代替.htaccess。

首先从请求中提取IP地址,然后

将IP地址与中间件中保存的值进行匹配。

如果匹配为true,则允许他们访问您的网站,否则将其重定向到其他页面。

答案 1 :(得分:0)

Middleware适合限制IP。 按照以下步骤创建Middleware并添加IP条件。如果IP将匹配,则将在404上重定向用户,否则请求将继续。

namespace App\Http\Middleware;

use Closure;

class IpMiddleware
{

    public function handle($request, Closure $next)
    {

        if ($request->ip() != "111.111.239.119") {
            // here insted checking single ip address we can do collection of ip 
            //address in constant file and check with in_array function
            return redirect('404');
        }

        return $next($request);
    }

}

创建中间件后,需要将此中间件添加到app/Http/Kernel.php数组的$middlewareGroups中。

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \App\Http\Middleware\IpMiddleware::class
    ],

    'api' => [
        'throttle:60,1',
    ],
];