slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.98k stars 1.95k forks source link

Uri::createFromEnvironment resolutions when using Server rewrites #1571

Closed piotr-cz closed 7 years ago

piotr-cz commented 9 years ago

Let's assume that the document root is set to ./, public folder resides in ./public (just like in slim3-skeleton app) and the .htaccess in the document root redirects all requests to public folder using [L] flag:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule .* public/$0 [L,QSA,E=PATH_INFO:/$0,E=PUBLIC_DIR:public]
</IfModule>

In this scenario base path resolution in Uri::createFromEnvironment doesn't work properly: real base path value instead of being stored in $basePath variable is stored in the $virtualPath.

I admit that this is very sketchy example and I use it only during local development when I'm too lazy to setup separate virutal host for each project.

The point is that using $_SERVER['SCRIPT_NAME'] and $_SERVER['REQUEST_URI'] for $basePath and $virtualPath resolutions will fail when using complex server rewrites.

One solution may be to provide public folder path as an environment variable (as I've done in the example), but more bullet-proof solution I'd like to see is that the method would respect project uri set manually in the container settings.

This could help solving another issue: the connection could be set manually as being secure or not as this is not trivial to autodetect when behind load balancer.

geggleto commented 9 years ago

You should be setting your webroot to the public folder, not using an .htaccess to redirect queries.

ppetermann commented 9 years ago

+1 on geggleto. public is called public, because it should contain all files exposed to public, as its the projects document root.

piotr-cz commented 9 years ago

I know. The point is how the base path resolution works now.

Image I'm using (invisible) server redirect to redirect http://foo.org/oldsite to http://foo.org/newsite on the server. The resolution mechanism assumes that request uri overlays physical path which is not correct.

So the user will see http://foo.org/oldsite in browser, $basePath will be set to '' and $virtualPath to newsite

akrabat commented 9 years ago

Arguably, one solution is to add middleware that does:

$request = $request->withBasePath('/newsite')->withPath('');
piotr-cz commented 9 years ago

@akrabat Yes, but if I'd like to use any routes in application I'd have to resolve the $virtualPath in middleware.

akrabat commented 9 years ago

I don't understand. That line would work the same way as using a config setting.

piotr-cz commented 9 years ago

Yes for no routes, but not for http://foo.org/oldsite/about

akrabat commented 9 years ago

Hmm... the point of setting it into BasePath was so that routes would work. I'll have to test it now!

akrabat commented 8 years ago

I have no idea what to actually do with this issue. What specific change do you propose should be made?

geggleto commented 8 years ago

This is such a huge edge case, I am for not doing anything.

codeguy commented 8 years ago

I would ignore this one. On Sun, Nov 29, 2015 at 9:23 AM Glenn Eggleton notifications@github.com wrote:

This is such a huge edge case, I am for not doing anything.

— Reply to this email directly or view it on GitHub https://github.com/slimphp/Slim/issues/1571#issuecomment-160419050.

piotr-cz commented 8 years ago

@akrabat I'd like to see is that the method would respect project uri set manually in the container settings.

But I don't mind closing the issue ATM either

akrabat commented 8 years ago

How about PR'ing what you imagine?

piotr-cz commented 8 years ago

I'll prepare PR, but it may take some time until I get some time

geggleto commented 7 years ago

Quite old, closing :)