【问题标题】:Is Apache RewriteRule interfering with If and Location directives?Apache RewriteRule 是否会干扰 If 和 Location 指令?
【发布时间】:2020-10-07 15:58:09
【问题描述】:

我正在使用 Apache 为 Laravel 站点实现 Shibboleth 单点登录。我想绕过身份验证中的一个特定 URL 子集(例如api/public),以便它们可以公开访问。由于某种原因,公用文件夹的 .htaccess 文件似乎阻止了它按预期工作。

我已经尝试了很多不同的方法,所有的道路都导致了同样的问题。这是我目前正在尝试的。

<Directory /var/www/html/mysite/public>
    SSLOptions +StdEnvVars
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    <If "%{REQUEST_URI} =~ m#api/public#">
        Require all granted
    </If>
    <Else>
        AuthType shibboleth
        ShibRequestSetting requireSession 1
        Require valid-user
    </Else>
</Directory>

这是 Laravel 附带的 .htaccess 文件:

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


    RewriteEngine On

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

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

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

不幸的是,.htaccess 文件已就位,Else 代码仍在执行,Shib 代码仍在运行。但我很确定 If 条件正在捕获,因为如果将 Require all denied 作为测试抛出,那么它确实会按预期禁止。

但是,如果我删除 .htaccess,这可行!但这也意味着 Laravel 中的任何路由现在都被破坏了,我确实需要这个面向公众的 URL。

我最好的猜测是.htaccessRewriteRule 导致Else 代码即使在If 语句被捕获后仍然运行。关于解决这个问题的任何建议?使用Location 指令遇到同样的问题。

谢谢。

【问题讨论】:

    标签: regex laravel apache .htaccess shibboleth


    【解决方案1】:

    想出了一个解决方案,将在这里分享,以防其他人遇到这个问题。问题确实是RewriteRule 导致我们再次遍历整个事情。 If 第一次按预期停止了 Shibboleth,但在 Laravel 将我们重定向到 index.php 并无论如何都要拿起它之后,我们会再次经历。

    通过将Else 更改为ElseIf 解决了这个问题,我们排除了index.php

    <Directory /var/www/html/mysite/public>
        SSLOptions +StdEnvVars
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        <If "%{REQUEST_URI} =~ m#api/public#">
            Require all granted
        </If>
        <ElseIf "%{REQUEST_URI} !~ m#index\.php#">
            AuthType shibboleth
            ShibRequestSetting requireSession 1
            Require valid-user
        </ElseIf>
    </Directory>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多