How to prevent apache reduction from double slashes with a single slash in the url?

Apache has a very nasty tendency to replace double slashes in a URL with one.

Example:

Request URL: http://example.com/myscript.php/foo//bar

When I look

 $_SERVER['PATH_INFO']; 

var, the path information will be displayed as:

 foo/bar 

instead

 foo//bar 

Does anyone know about this? I believe this is rooted somewhere in apache functionality ... I don’t know if there is any apache flag that can be configured to disable this behavior.

+4
source share
3 answers

This is part of the RFC standard for resolving URIs, so you cannot change this.

Even your browser probably normalizes the URI before sending the request to the remote server.

0
source

nginx has a merge_slashes directive that allows you to merge slices according to location, and is disabled by default, which means that it is not merged by default. If merge behavior is specified in the RFC, it certainly does not follow nginx.

0
source

http://example.com/myscript.php/foo//bar

/foo//bar - additional information about the path that follows the actual file name. While Apache reduces multiple slashes in the PATH_INFO server variable (which is passed to the corresponding superglobal PHP), the original URL (with several slashes) is still available in the $ _SERVER ['PHP_SELF'] variable.

So, instead of accessing the path information through the PATH_INFO variable, you can do something like the following:

 $pathInfo = str_replace($_SERVER['SCRIPT_NAME'],'',$_SERVER['PHP_SELF']); 

It just removes SCRIPT_NAME from PHP_SELF, leaving the path information (if any). You can use REQUEST_URI instead of PHP_SELF, but this includes a query string, so you will need to check this.

So, given the above request, where SCRIPT_NAME is "/myscript.php" and PHP_SELF is "/myscript.php/foo//bar", the resulting $pathInfo is "/ foo // bar".

0
source

Source: https://habr.com/ru/post/1434056/


All Articles