Eugene
Eugene

Reputation: 2974

How to prevent apache from reducing double slashes with single slash in a URL path?

Apache has a very annoying tendancy to replace double slashes in the URL with a single.

Example:

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

When I look at the

$_SERVER['PATH_INFO']; 

var, the path info will show up as:

foo/bar

instead of

foo//bar

Does anyone know of a fix for this? I believe this is ingrained somewhere in apache's functionality... I don't know if there's some kind of an apache flag that can be tweaked to disable this behavior.

Upvotes: 4

Views: 2346

Answers (3)

MrWhite
MrWhite

Reputation: 45829

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

The /foo//bar is the additional path information that follows an actual filename. Whilst Apache does reduce the multiple slashes in the PATH_INFO server variable (which is passed through to the corresponding PHP superglobal), the original URL (with multiple slashes) is still available in the $_SERVER['PHP_SELF'] variable.

So, instead of accessing the path info via the PATH_INFO variable, you could do something like the following instead:

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

This simply removes the SCRIPT_NAME from PHP_SELF, leaving the path-info (if any). You could use REQUEST_URI instead of PHP_SELF, but this includes the query string, so you would need to check for this.

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

Upvotes: 1

Jack Peng
Jack Peng

Reputation: 632

nginx has a merge_slashes directive that allows slashes to be merged to match a location and it defaults to off, meaning it doesn't merge by default. If the merging behavior is specified in the RFC, it's certainly not followed by nginx.

Upvotes: 0

Maks3w
Maks3w

Reputation: 6429

It's part of RFC standard for resolve URIs so you can't change that.

Even probably your browser normalize the URI before send the request to the remote server.

Upvotes: 0

Related Questions