V
V
valentine112018-10-08 11:26:59
Nginx
valentine11, 2018-10-08 11:26:59

Double slash instead of one in uri - is there a difference?

If, due to some kind of jamb, when forming a request, say, to api, instead of one slash, there are two slashes in some place in the request - does it matter? Will this lead to random errors or longer processing of the request by the server? Or some other side effect?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Decadal, 2018-10-08
@Decadal

depending on how the routing is done. In most cases, this is a critical error that will result in 404 \ 403

A
Alexey Arkh, 2020-12-14
@AlekseyArh

nginx merges slashes by default - merge_slashes off; // on by default
This can lead to surprises.
For example, if you are using nginx cache, the cache key is built from the url.
You will go to site/page.html, and 404 is cached there, because the crooked parser went to site//page.html, and your backing gave nginx 404, which it cached into the md5 key (site/page.html)
Plus it's crooked a request for a back, which in idle will look for a page, which is so clear that there is none.
The treatment option I see at the moment is to add merge_slashes off; before the server
section And at the beginning of the server section, write two regular expressions.

rewrite ^//([^/].*) /$1 permanent;
rewrite (.*)//+(.*) /404 permanent;

The first will work if there are two slashes at the beginning of the link (site//page.html) and redirect to one slash (site/page.html)
This is a common mistake, for example, the user copied a relative link, pasted it into the address bar, and the domain was already filled there.
Or parsers that parse crookedly.
The second regular expression will work if the first one did not work, and if there are two or more slashes in the remaining url, then we consider this an error and redirect to the prepared 404 page
This site//page.html will be corrected to this site/page.html
This is site/cat// page.html to /404
This url site/page.html?utm= https://site does not fall under regular expressions.
Only one regular expression can be used
rewrite (.*)//+(.*) $1/$2 permanent;
Then all double slashes will be changed to single slashes (except for link parameters)
But this is a new redirect every time. You will end up with ERR_TOO_MANY_REDIRECTS It's
5fd794a2b921f587081961.png
better not to do this, but just treat crooked links as crooked.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question