Problem with subfolders and url rewrite with Laravel

4

I'm trying to make Laravel 4 run in a subdirectory, using .htaccess to be able to rewrite the url to the public folder

Example:

  • C:\xammp\htdocs\laravel4
  • .htaccess is rewriting C:\xammp\htdocs\laravel4 to C:\xammp\htdocs\laravel4\public

Works correctly for rewriting. However, when I try to create a setting in the routes.php file, it always returns a page not found.

Example:

If I access localhost/laravel4/teste , with the configuration below, Laravel says the page was not found.

Route::get('/teste', function(){

 return "Hello World";
});

Now, when I do it this way, it shows correctly:

Route::get('laravel4/teste', function(){
   return "Hello Word";
});

Between tests and others, I discovered that Laravel 4, when it is going to route the url, does not use $_SERVER['PATH_INFO'] (which would only return the parameters after the public / index.php rewritten), but yes $_SERVER['REQUEST_URI'] (which would return everything that comes after http://localhost/ ).

So I can rewrite the url, but I can not configure the routes normally, but only when I use a prefix for the subfolder I'm using.

How to solve this problem without having to change core of Laravel 4?

Is there any way to bypass or prefix these routes internally (Any method in the Route class or some global configuration)?

    
asked by anonymous 11.12.2014 / 17:46

1 answer

1

Well, after trying, trying and trying, I ended up appealing to the following solution.

In case you want to configure Laravel 4 within a subdirectory, you have to make a small change in the bootstrap/start.php folder and add the following code before everything is declared:

if (isset($_SERVER['REQUEST_URI'])) {

    $prefixed_uri = basename(dirname(__DIR__)) . $_SERVER['REQUEST_URI'];

    $_SERVER['HTTP_X_ORIGINAL_URL'] = $prefixed_uri;

}

I do this because the Symfony\Component\HttpFoundation\Request , which is used in Laravel 4 , uses, in the prepareRequestUri method, the header HTTP_X_ORIGINAL_URL to get the current URI.

I do not know if setting this variable can cause a problem, but that was the only solution found.

    
25.02.2015 / 18:24