How to send a URL in route parameter?

2020-03-30 06:53发布

I have defined a route like this :

$app->map(['GET', 'POST'],'/abc/[{url}]', function ($request, $response, $args) {

    return $response;
})->add(new CustomMiddleware());

Its working fine when I pass a url without http:// but gives me a 404 page not found-Page with http:// or https://. I have also tried with url encoded string but gives same error :

http://localhost/slim/public/index.php/abc/http%3A%2F%2Fstackoverflow.com

The requested URL /slim/public/index.php/abc/http://stackoverflow.com was not found on this server.

I am using Slim Version 3.1.

标签: php slim slim-3
1条回答
可以哭但决不认输i
2楼-- · 2020-03-30 07:23

Using an url inside the url

When you adding the url with Slashes then the route do not get execute cause then there is additional path after the url which is not definied inside the route:

E.g. example.org/abc/test works fine but example.org/abc/http://x will only work with a route definition like this /abc/{url}//{other}.

Using an encoded url inside the url

Apache blocks all request with %5C for \ and %2F for / in the url with a 404 Not Found error this is because of security reasons. So you do not get a 404 from the slim framework but from your webserver. So you'r code never gets executed.

You can enable this by setting AllowEncodedSlashes On in you'r httpd.conf of apache.

My Recommendation to fix this

Add the url as a get parameter there is is valid to have encode slashes without changing the apache config.

Example call http://localhost/abc?url=http%3A%2F%2Fstackoverflow.com

$app->map( ['GET', 'POST'], '/abc', function ($request, $response, $args) {
    $getParam = $request->getQueryParams();
    $url= $getParam['url']; // is equal to http://stackoverflow.com
});
查看更多
登录 后发表回答