在Laravel 5中如何允许和拒绝.htacces文件? [英] How in Laravel 5 to allow and deny in .htacces file?

查看:56
本文介绍了在Laravel 5中如何允许和拒绝.htacces文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的.htacces文件

this my .htacces file

<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的新手,请帮忙,谢谢

I want to allow only a few IP that can access my website, is there an alternative way to use other than htacces, I'm very newbie at Laravel please help, thanks

推荐答案

中间件对于IP限制很有用.按照以下步骤创建 Middleware 并添加IP条件.如果IP将匹配,则将在404上重定向用户,否则请求将继续.

Middleware is good for restriction of IP. Create Middleware as per following and add a condition for IP, If IP will match then a user will be redirected on 404 otherwise request will continue.

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);
    }

}

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

After creating middleware, Need to add this middleware in app/Http/Kernel.php in $middlewareGroups array.

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

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

这篇关于在Laravel 5中如何允许和拒绝.htacces文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆