How to send a URL in route parameter?

让人想犯罪 __ 提交于 2019-12-01 12:01:50

问题


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.


回答1:


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
});


来源:https://stackoverflow.com/questions/39362243/how-to-send-a-url-in-route-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!