Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX - Remove double slash after .com

Tags:

nginx

I've got a few double slashes I can't get my nginx.conf file to rewrite as single slashes. I'm able to get it to rewrite a majority of double slashes, for example I can get http://domain.com/feature//feature-name/ to http://domain.com/feature/feature-name/

if ($request_uri ~ "^[^?]*//") {
    rewrite "(.*)" $scheme://$host$1 permanent;
}

The issue I'm having is that if I have a double slash immediately following the .com, it won't rewrite to a single slash. My two examples that I'm having trouble with are http://domain.com// and http://domain.com//features/

I've tried using the merge_slashes directive, and a variety of other rewrites. I'm just at a loss as to why they all seem to work EXCEPT right after the .com. Thanks!

like image 426
heatherthedev Avatar asked Sep 14 '25 04:09

heatherthedev


1 Answers

The real question is : how did you end up with such URLs ?

If you use nginx normally you will not get such a behaviour as nginx is normalizing URIs (i.e. it will compress several adjacent slashes in one, decode URLs and resolve relative paths).

The normalization result of the URI from the current request being processed is stored in $uri variable.

So the following part should never be necessary.

if ($request_uri ~ "//") {
    return 301 $uri;
}
like image 172
Xavier Lucas Avatar answered Sep 16 '25 22:09

Xavier Lucas