Subfolder problem and url rewriting with Laravel

Asked

Viewed 1,106 times

4

I’m trying to do the Laravel 4 run in a subdirectory, using . htaccess to rewrite the url to the public folder

Example:

  • C:\xammp\htdocs\laravel4
  • The .htaccess is in rewriting C:\xammp\htdocs\laravel4 for C:\xammp\htdocs\laravel4\public

Works properly for the rewrite. However, when I try the file routes.php create a configuration, always returns page not found.

Example:

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

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

 return "Hello World";
});

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

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

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

So I can rewrite the url, but I can’t configure the routes normally, but only when I use a prefix referring to the subfolder I’m using.

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

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

  • Try with teste instead of /teste.

  • @Rodrigorigotti, I did this, but the result was the same (only works with the unwanted prefix). Looks like, internally, Laravel4 already handles those bars.

  • 1

    I don’t think it’s a good idea to expose your entire application in an internet-free folder. You can make a vhost pointing directly to the public folder of the Laravel, besides safer will not have other problems

  • @gmsantos, I also thought about this possibility, but, in local development, it is good to adapt the application to what it will be in production; and, in production, on a shared server, no one has access to a vhost.

1 answer

1


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

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

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

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

    $_SERVER['HTTP_X_ORIGINAL_URL'] = $prefixed_uri;

}

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

I don’t know if defining this variable can cause any problems, but this was the only solution found.

  • 1

    I know! That’s a great gambit.

  • Other interesting ways to do this http://driesvints.com/blog/laravel-4-on-a-shared-host (I particularly like the first one)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.