【问题标题】:Laravel sets wrong public path with a index.php file is requested in a subdirectoryLaravel 设置了错误的公共路径,在子目录中请求了 index.php 文件
【发布时间】:2016-01-08 13:25:04
【问题描述】:

在使用 Apache 2.4 的 Laravel 4.2 中,如果我在站点根目录中访问的 index.phpindex.php 不同,则公共路径设置错误。比如/site/index.php返回的html文档是对的,但是它所有的assets路由都是相对于/site的。例如,

文件系统路径:/var/www/htdocs/imgs/logo.png

正确的网址:/imgs/logo.png

真实路径:/site/imgs/logo.png

发现这个问题是因为旧网站有您在/site/index.php 中的索引文件并被搜索引擎索引。然后,当该站点显示在搜索结果中时,它会显示一个指向 site/index.php 的链接,其中所有资产路径都已损坏。

资产的路径使用 Laravel 的函数设置为 HTML::styleassets

如何将子路径中的所有 index.php 文件重定向到根文件中的 index.php,除了最后一个?

【问题讨论】:

    标签: laravel redirect path public


    【解决方案1】:

    Laravel 4.2 默认的.htaccess

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        # Redirect Trailing Slashes...
        RewriteRule ^(.*)/$ /$1 [L,R=301]
    
        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    </IfModule>
    

    然后我在#Handle Front Controller之前添加一个新的重写规则

    # Redirect to index.php if other index.php distict to the index.php is requested.
    RewriteCond %{REQUEST_URI} ((.+)/)+index.php [NC]
    RewriteRule ^ / [R=301]
    

    因此,我将(永久)重定向到站点根目录到子目录中的任何 index.php

    【讨论】:

      猜你喜欢
      • 2013-06-05
      • 2017-10-27
      • 2021-07-10
      • 2013-01-01
      • 2018-12-01
      • 2015-03-28
      • 2016-07-13
      • 1970-01-01
      • 2013-05-08
      相关资源
      最近更新 更多