首页 文章

Laravel设置错误的公共路径,并在子目录中请求index.php文件

提问于
浏览
0

在使用Apache 2.4的Laravel 4.2中,如果我访问与站点根目录中的index.php不同的index.php,则公共路径设置错误 . 例如, /site/index.php 返回的html文档是正确的,但它到资产的所有路由都是相对于 /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,除了最后一个?

1 回答

  • 0

    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 .

相关问题