繁体   English   中英

Laravel的Apache Rewrite规则不起作用

[英]Apache Rewrite rule with Laravel not working

我有以下目录结构:

/home/username/public_html
        my_laravel_app/
            .htaccess
            app/
            bootstrap/
            public/
                .htaccess
                index.php
                robots.txt
                ...etc (standard Laravel 4 public folder contents)
            vendor/
            ...etc (standard Laravel 4 files and folders)

我正在尝试使用一个Apache RewriteRule将请求重定向到http://example.com/~user/my_laravel_apphttp://example.com/~user/my_laravel_app/public/index.php 在第一个.htaccess文件(位于my_laravel_app/目录下的文件)中,我具有以下代码:

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /~username/my_laravel_app
        RewriteCond %{THE_REQUEST} !public/
        RewriteRule ^(.*)$ public/index.php [R]
</IfModule>

当我将浏览器导航到http://example.com/~username/my_laravel_app ,我正确地重定向到了http://example.com/~user/my_laravel_app/public/index.php ,并且laravel应用程序按预期加载显示/的路线,如app/routes.php所定义。

但是,我希望重定向是内部的,也就是说,我不希望用户浏览器显示新位置。 因此,我只是删除规则上的[R]标志。 .htaccess文件现在包含:

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /~username/my_laravel_app
        RewriteCond %{THE_REQUEST} !public/
        RewriteRule ^(.*)$ public/index.php
</IfModule>

现在,当我将浏览器定向到http://example.com/~username/my_laravel_app我得到一个Laravel糟糕的错误页面,上面说有一个NotFoundHttpException 当我查看错误页面时,似乎重定向正在“起作用”,因为我看到SCRIPT_NAME/~username/my_laravel_app/public/index.php

但是,我认为问题可能是REQUEST_URI/~username/my_laravel_app/ ,而不是'/',就像Laravel像通常那样安装在域的根目录中一样。 因此,似乎Laravel正在使用此REQUEST_URI变量来寻找路线匹配。

有什么方法可以让Laravel识别正确的路线?

我在Laravel 4.1上遇到了同样的问题,并且似乎与我的apache配置有关,但是,我确实找到了一种解决方法,即将以下内容添加到public / index.php的顶部:

$ _SERVER ['SCRIPT_NAME'] ='路径/到/laravel/index.php';

// //注意:这是mod_rewrite和laravel的变通方法,可将script_name从/path/to/laravel/public/index.php更新为/path/to/laravel/index.php。 我确切地忘记了为什么这行得通,但是我认为当我追溯问题时,它与symfony依赖项有关。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM