[英]Apache Rewrite rule with Laravel not working
I have the following directory structure: 我有以下目录结构:
/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)
I am trying to use an apache RewriteRule to redirect a request to http://example.com/~user/my_laravel_app
to http://example.com/~user/my_laravel_app/public/index.php
. 我正在尝试使用一个Apache RewriteRule将请求重定向到
http://example.com/~user/my_laravel_app
到http://example.com/~user/my_laravel_app/public/index.php
。 In the first .htaccess
file (the one directly under the my_laravel_app/
directory, I have the following code: 在第一个
.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>
When I navigate my browser to http://example.com/~username/my_laravel_app
, I correctly get redirected to http://example.com/~user/my_laravel_app/public/index.php
, and the laravel application loads as expected displaying the route for /
as defined in app/routes.php
. 当我将浏览器导航到
http://example.com/~username/my_laravel_app
,我正确地重定向到了http://example.com/~user/my_laravel_app/public/index.php
,并且laravel应用程序按预期加载显示/
的路线,如app/routes.php
所定义。
However, I want the redirect to be internal, meaning, I don't want the users browser to display the new location. 但是,我希望重定向是内部的,也就是说,我不希望用户浏览器显示新位置。 So, I simply remove the
[R]
flag on the rule. 因此,我只是删除规则上的
[R]
标志。 The .htaccess
file now contains: .htaccess
文件现在包含:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~username/my_laravel_app
RewriteCond %{THE_REQUEST} !public/
RewriteRule ^(.*)$ public/index.php
</IfModule>
Now, when I direct the browser to http://example.com/~username/my_laravel_app
I get a Laravel whoops error page saying there was a NotFoundHttpException
. 现在,当我将浏览器定向到
http://example.com/~username/my_laravel_app
我得到一个Laravel糟糕的错误页面,上面说有一个NotFoundHttpException
。 When I look at the error page, it seems as though the redirect is "working", because I see that the SCRIPT_NAME
is /~username/my_laravel_app/public/index.php
. 当我查看错误页面时,似乎重定向正在“起作用”,因为我看到
SCRIPT_NAME
是/~username/my_laravel_app/public/index.php
。
However, I believe the issue is possibly that the REQUEST_URI
is /~username/my_laravel_app/
, instead of '/', as it would be if Laravel were installed in the root of a domain like normal. 但是,我认为问题可能是
REQUEST_URI
是/~username/my_laravel_app/
,而不是'/',就像Laravel像通常那样安装在域的根目录中一样。 So it seems as though Laravel is looking for a route match using this REQUEST_URI
variable. 因此,似乎Laravel正在使用此
REQUEST_URI
变量来寻找路线匹配。
Is there any way to get Laravel to recognize the correct route? 有什么方法可以让Laravel识别正确的路线?
I had the same issue on Laravel 4.1, and appears related to my apache config, however, I did find a workaround which was to add the following to the top of the public/index.php: 我在Laravel 4.1上遇到了同样的问题,并且似乎与我的apache配置有关,但是,我确实找到了一种解决方法,即将以下内容添加到public / index.php的顶部:
$_SERVER['SCRIPT_NAME'] = 'path/to/laravel/index.php'; $ _SERVER ['SCRIPT_NAME'] ='路径/到/laravel/index.php';
// note: this is a workaround for mod_rewrite and laravel which updates the script_name from /path/to/laravel/public/index.php to /path/to/laravel/index.php. // //注意:这是mod_rewrite和laravel的变通方法,可将script_name从/path/to/laravel/public/index.php更新为/path/to/laravel/index.php。 I forget exactly why this worked, but I think when I traced the problem back it was related to something in a symfony dependency.
我确切地忘记了为什么这行得通,但是我认为当我追溯问题时,它与symfony依赖项有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.