【问题标题】:Laravel .htaccess rewrite and apache2 Location directiveLaravel .htaccess 重写和 apache2 Location 指令
【发布时间】:2017-03-30 08:33:06
【问题描述】:

我正在尝试使用 Apache 限制对我的 Laravel 5.3 应用程序的访问。

我的应用在内部可用,但会收到来自各种外部来源的回传。有一个禁用 NAT 的端口转发设置,因此我可以区分内部请求和外部请求。

所有 URL 都应显示 403,除非 example.com/api/external/... 是 URL。我有以下 htaccess(Laravel 5.3 的默认设置)

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

# 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]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

我的虚拟主机配置

<Directory /var/www/mailer/public/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
    </Directory>

    <Location />
            Order deny,allow
            deny from all
            allow from 10.64.1.0/24
            allow from 10.64.20.0/24
    </Location>

    <Location /api/external/smsPost>
            Allow from all
    </Location>

每当我从外部地址访问任何 URL 时,即使在允许的位置,我仍然会收到 403。

You don't have permission to access /index.php on this server.

我认为因为它甚至在 example.com/api/external/smsPost 上都有 /index.php,所以这是 htaccess 的问题,并且 location 指令不适用于此目的。有什么方法可以用这个指令实现我所需要的吗?

非常感谢。

【问题讨论】:

    标签: .htaccess laravel apache2


    【解决方案1】:

    您为什么不使用 laravel 中间件并将其绑定到您的受限路由?

    public function handle($request, Closure $next)
    {
    
         if(Route::getCurrentRoute()->getPath() == "api/external/") 
               return response('You don't have permission to access /index.php on this server.', 403);
    
         // return the $next closure, so other Middlewares can run
         return $next($request);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-08
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 2015-08-22
      相关资源
      最近更新 更多