简体   繁体   English

Laravel Redirect :: route()在虚拟主机上进入错误的URL

[英]Laravel Redirect::route() going to wrong url on vhost

I was working on some code today and sent a Redirect::route() . 我今天正在处理一些代码,并发送了Redirect::route() Instead of redirecting to the base_url/route as usual, it duplicated the base_url like this: 它没有像通常那样重定向到base_url / route,而是像这样复制了base_url:

http://myurl.dev/http://myurl.dev/myroute http://myurl.dev/http://myurl.dev/myroute

I figured I did something wrong, so I went back and tried to isolate the problem. 我以为自己做错了什么,所以我回过头来试图找出问题所在。 I ended up starting a new project with a new vhost and putting this tiny bit of code in app/routes.php : 我最终以一个新的虚拟主机开始了一个新项目,并将这小部分代码放入app / routes.php中

Route::get(
    'test1',
    [
        'as' => 'test1',
        function () {
            return Redirect::route('test2');
        }
    ]
);

Route::get(
    'test2',
    [
        'as' => 'test2',
        function () {
            return 'test2hello';
        }
    ]
);

When I open http://myurl.dev/test1 in a browser, instead of just showing "test2hello" it threw and http not found error because http://myurl.dev/http://myurl.dev/test2 was not found. 当我在浏览器中打开http://myurl.dev/test1时,它并没有显示“ test2hello”,而是抛出了http并没有发现错误,因为没有找到http://myurl.dev/http://myurl.dev/test2找到。 This only happens on Redirect::route() , it works as expected on Redirect::to() . 这仅在Redirect::route() ,它按预期在Redirect::to() It also only happens on vhosts; 它也仅在虚拟主机上发生。 Redirect::route() works as expected if I go to localhost/myurl/public/test1. 如果我转到localhost / myurl / public / test1,则Redirect::route()按预期工作。 Any ideas? 有任何想法吗?


UPDATE: 更新:

I was asked for my vhost setup. 我被要求进行我的虚拟主机设置。 I am on Mac OSX 10.8.5 and am using the built-in Apache. 我在Mac OSX 10.8.5上,并且正在使用内置的Apache。 I've uncommented the httpd-vhosts.conf include line in /etc/apache2/httpd.conf . 我没有评论 /etc/apache2/httpd.conf中httpd-vhosts.conf包含行。 I've added a few vhosts to /etc/apache2/extra/httpd-vhosts.conf , here's one: 我在/etc/apache2/extra/httpd-vhosts.conf中添加了几个虚拟主机,这是一个:

<VirtualHost *:80>
    DocumentRoot "/Library/WebServer/Documents/example_blog/public"
    ServerName example_blog.local
</VirtualHost>

and the corresponding line in /etc/hosts : 以及/ etc / hosts中的相应行:

127.0.0.1   example_blog.local

and restarted Apache. 并重新启动Apache。 The folder is named example_blog.local . 该文件夹名为example_blog.local

Problem appears to be related to having an underscore in the URL, which does not pass FILTER_VALID_URL: 问题似乎与URL中带有下划线(未通过FILTER_VALID_URL)有关:

https://github.com/laravel/framework/issues/2511 https://github.com/laravel/framework/issues/2511

(no credit to me for the answer as I'm just tidying up this to help others searching for solutions to this) (由于我正在整理此内容以帮助其他人寻找解决方案,因此我的回答不值得我信任)

Try this method : 试试这个方法:

Route::get(
    'test1',
    [
        'as' => 'test1',
        function () {
            return Response::make('', 302)->header('Location', route('test2'));
        }
    ]
);

Route::get(
    'test2',
    [
        'as' => 'test2',
        function () {
            return 'test2hello';
        }
    ]
);

as you can see we use Response Class which Redirect itself uses it to send header location :) 如您所见,我们使用Response类, Redirect自身使用它来发送标头位置:)

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

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