【问题标题】:Redirect to homepage if page is a directory listing in Laravel如果页面是 Laravel 中的目录列表,则重定向到主页
【发布时间】:2017-03-19 18:09:51
【问题描述】:

如果用户所在的页面是目录列表,我希望能够将用户重定向(到 404 页面或返回主页),如下所示:

这是我的 .htaccess 文件:

<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]
Options +FollowSymLinks
RewriteEngine On

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

这对 Laravel 可行吗?我知道您可以重定向到 app/Exceptions/Handler.php 文件中的特定页面,以防出现特定错误,例如 404 或 500。也许可以使目录生成错误,并让 handler.php 进行重定向?

【问题讨论】:

    标签: apache laravel .htaccess laravel-5.1


    【解决方案1】:

    这不是严格意义上的 Laravel 问题,目录列表由 Apache 直接提供,而 Laravel 甚至没有加载。

    作为一种简单的解决方法,您可以在目录中放置一个最低限度的 index.html 并为重定向设置一个元数据,即:

    <meta http-equiv="refresh" content="0;URL=http://www.example.com/example">
    

    更好的解决方案是正确配置Apache,你可以找到一些信息here

    【讨论】:

    • 对,目录中有一个 index.html 文件会阻止目录查看,但是在这种情况下,我希望重定向,而不仅仅是阻止查看。
    • 如果在 index.html 中有我上面写的元刷新,你可以重定向到你想要的地方
    【解决方案2】:

    你可以使用一些东西做一个 php 重定向

        <?php
        // PHP permanent URL redirection test
        header("Location: http://www.rapidtables.com/web/dev/php-redirect.htm", true, 301);
        exit();
        ?>
    

    你看到了吗:This tutorial?

    【讨论】:

    • 这并不是那么有用。我如何能够将 php 重定向放在目录列表中?
    • 在 dparoli 的回答中,您可以在打开 index.html 时让 php 脚本运行
    猜你喜欢
    • 2014-06-10
    • 2015-09-04
    • 2020-05-20
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多