How to remove double slash in URLs served by nginx?












8















I need to replicate the following Apache rewrite rules in Nginx config on Ubuntu 12.04. What is the nginx equivalent to :



RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]









share|improve this question

























  • stackoverflow.com/questions/14832780/…

    – cnst
    Mar 23 '13 at 22:28











  • Whoever found this online and tries to copy-paste the suggested answers, beware if you're using a Virtualbox setup with custom ports. I had to change it to be e.g. rewrite (.*)//+(.*) $scheme://$host:4321$1/$2 permanent; where 4321 is the external port of the Virtualbox'd nginx my browser connects to.

    – aexl
    Nov 20 '17 at 21:53


















8















I need to replicate the following Apache rewrite rules in Nginx config on Ubuntu 12.04. What is the nginx equivalent to :



RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]









share|improve this question

























  • stackoverflow.com/questions/14832780/…

    – cnst
    Mar 23 '13 at 22:28











  • Whoever found this online and tries to copy-paste the suggested answers, beware if you're using a Virtualbox setup with custom ports. I had to change it to be e.g. rewrite (.*)//+(.*) $scheme://$host:4321$1/$2 permanent; where 4321 is the external port of the Virtualbox'd nginx my browser connects to.

    – aexl
    Nov 20 '17 at 21:53
















8












8








8


3






I need to replicate the following Apache rewrite rules in Nginx config on Ubuntu 12.04. What is the nginx equivalent to :



RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]









share|improve this question
















I need to replicate the following Apache rewrite rules in Nginx config on Ubuntu 12.04. What is the nginx equivalent to :



RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]






nginx rewrite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 25 at 11:39









JakeGould

32k1098140




32k1098140










asked Mar 13 '13 at 13:16









codecowboycodecowboy

2403927




2403927













  • stackoverflow.com/questions/14832780/…

    – cnst
    Mar 23 '13 at 22:28











  • Whoever found this online and tries to copy-paste the suggested answers, beware if you're using a Virtualbox setup with custom ports. I had to change it to be e.g. rewrite (.*)//+(.*) $scheme://$host:4321$1/$2 permanent; where 4321 is the external port of the Virtualbox'd nginx my browser connects to.

    – aexl
    Nov 20 '17 at 21:53





















  • stackoverflow.com/questions/14832780/…

    – cnst
    Mar 23 '13 at 22:28











  • Whoever found this online and tries to copy-paste the suggested answers, beware if you're using a Virtualbox setup with custom ports. I had to change it to be e.g. rewrite (.*)//+(.*) $scheme://$host:4321$1/$2 permanent; where 4321 is the external port of the Virtualbox'd nginx my browser connects to.

    – aexl
    Nov 20 '17 at 21:53



















stackoverflow.com/questions/14832780/…

– cnst
Mar 23 '13 at 22:28





stackoverflow.com/questions/14832780/…

– cnst
Mar 23 '13 at 22:28













Whoever found this online and tries to copy-paste the suggested answers, beware if you're using a Virtualbox setup with custom ports. I had to change it to be e.g. rewrite (.*)//+(.*) $scheme://$host:4321$1/$2 permanent; where 4321 is the external port of the Virtualbox'd nginx my browser connects to.

– aexl
Nov 20 '17 at 21:53







Whoever found this online and tries to copy-paste the suggested answers, beware if you're using a Virtualbox setup with custom ports. I had to change it to be e.g. rewrite (.*)//+(.*) $scheme://$host:4321$1/$2 permanent; where 4321 is the external port of the Virtualbox'd nginx my browser connects to.

– aexl
Nov 20 '17 at 21:53












4 Answers
4






active

oldest

votes


















5














I'd like to suggest this approach:



# remove multiple sequences of forward slashes
# rewrite URI has duplicate slashes already removed by Nginx (merge_slashes on), just need to rewrite back to current location
# note: the use of "^[^?]*?" avoids matches in querystring portion which would cause an infinite redirect loop
if ($request_uri ~ "^[^?]*?//") {
rewrite "^" $scheme://$host$uri permanent;
}


It uses the default behaviour of nginx — merging of slashes, so we do not need to replace slashes, we simply redirecting



found here






share|improve this answer



















  • 1





    'if' is evil: nginx.com/resources/wiki/start/topics/depth/ifisevil

    – MacroMan
    Feb 14 '18 at 11:47



















2














I found kwo's response to not work. Looking at my debug log, this is what happens:



2014/08/18 15:51:04 [debug] 16361#0: *1 http script regex: "(.*)//+(.*)"
2014/08/18 15:51:04 [notice] 16361#0: *1 "(.*)//+(.*)" does not match "/contact-us/", client: 59.167.230.186, server: *.domain.edu, request: "GET //////contact-us//// HTTP/1.1", host:
"test.domain.edu"


I found this worked for me:



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


Ref: http://rosslawley.co.uk/archive/old/2010/01/10/nginx-how-to-url-cleaning-removing/






share|improve this answer


























  • Reference link is what you need to check. Correct solution is in there. I will try to edit the answer.

    – Anup Nair
    Apr 30 '18 at 7:36



















0














Try this:



merge_slashes off;
rewrite (.*)//+(.*) $1/$2 permanent;


There might be multiple redirects for slashes > 3 or multiple groups of slashes.






share|improve this answer
























  • 'merge_slashes off' made no difference and resulted in no change.

    – Anup Nair
    Apr 30 '18 at 7:34



















-1














URL example.com//dir1////dir2///dir3 and more
Try this it's working for me



merge_slashes off;
location ~ ^(.*?)//+(.*?)$ {
return 301 $1/$2;
}






share|improve this answer
























    protected by JakeGould Jan 25 at 11:40



    Thank you for your interest in this question.
    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



    Would you like to answer one of these unanswered questions instead?














    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    I'd like to suggest this approach:



    # remove multiple sequences of forward slashes
    # rewrite URI has duplicate slashes already removed by Nginx (merge_slashes on), just need to rewrite back to current location
    # note: the use of "^[^?]*?" avoids matches in querystring portion which would cause an infinite redirect loop
    if ($request_uri ~ "^[^?]*?//") {
    rewrite "^" $scheme://$host$uri permanent;
    }


    It uses the default behaviour of nginx — merging of slashes, so we do not need to replace slashes, we simply redirecting



    found here






    share|improve this answer



















    • 1





      'if' is evil: nginx.com/resources/wiki/start/topics/depth/ifisevil

      – MacroMan
      Feb 14 '18 at 11:47
















    5














    I'd like to suggest this approach:



    # remove multiple sequences of forward slashes
    # rewrite URI has duplicate slashes already removed by Nginx (merge_slashes on), just need to rewrite back to current location
    # note: the use of "^[^?]*?" avoids matches in querystring portion which would cause an infinite redirect loop
    if ($request_uri ~ "^[^?]*?//") {
    rewrite "^" $scheme://$host$uri permanent;
    }


    It uses the default behaviour of nginx — merging of slashes, so we do not need to replace slashes, we simply redirecting



    found here






    share|improve this answer



















    • 1





      'if' is evil: nginx.com/resources/wiki/start/topics/depth/ifisevil

      – MacroMan
      Feb 14 '18 at 11:47














    5












    5








    5







    I'd like to suggest this approach:



    # remove multiple sequences of forward slashes
    # rewrite URI has duplicate slashes already removed by Nginx (merge_slashes on), just need to rewrite back to current location
    # note: the use of "^[^?]*?" avoids matches in querystring portion which would cause an infinite redirect loop
    if ($request_uri ~ "^[^?]*?//") {
    rewrite "^" $scheme://$host$uri permanent;
    }


    It uses the default behaviour of nginx — merging of slashes, so we do not need to replace slashes, we simply redirecting



    found here






    share|improve this answer













    I'd like to suggest this approach:



    # remove multiple sequences of forward slashes
    # rewrite URI has duplicate slashes already removed by Nginx (merge_slashes on), just need to rewrite back to current location
    # note: the use of "^[^?]*?" avoids matches in querystring portion which would cause an infinite redirect loop
    if ($request_uri ~ "^[^?]*?//") {
    rewrite "^" $scheme://$host$uri permanent;
    }


    It uses the default behaviour of nginx — merging of slashes, so we do not need to replace slashes, we simply redirecting



    found here







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 14 '14 at 7:48









    SleepWalkerSleepWalker

    15112




    15112








    • 1





      'if' is evil: nginx.com/resources/wiki/start/topics/depth/ifisevil

      – MacroMan
      Feb 14 '18 at 11:47














    • 1





      'if' is evil: nginx.com/resources/wiki/start/topics/depth/ifisevil

      – MacroMan
      Feb 14 '18 at 11:47








    1




    1





    'if' is evil: nginx.com/resources/wiki/start/topics/depth/ifisevil

    – MacroMan
    Feb 14 '18 at 11:47





    'if' is evil: nginx.com/resources/wiki/start/topics/depth/ifisevil

    – MacroMan
    Feb 14 '18 at 11:47













    2














    I found kwo's response to not work. Looking at my debug log, this is what happens:



    2014/08/18 15:51:04 [debug] 16361#0: *1 http script regex: "(.*)//+(.*)"
    2014/08/18 15:51:04 [notice] 16361#0: *1 "(.*)//+(.*)" does not match "/contact-us/", client: 59.167.230.186, server: *.domain.edu, request: "GET //////contact-us//// HTTP/1.1", host:
    "test.domain.edu"


    I found this worked for me:



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


    Ref: http://rosslawley.co.uk/archive/old/2010/01/10/nginx-how-to-url-cleaning-removing/






    share|improve this answer


























    • Reference link is what you need to check. Correct solution is in there. I will try to edit the answer.

      – Anup Nair
      Apr 30 '18 at 7:36
















    2














    I found kwo's response to not work. Looking at my debug log, this is what happens:



    2014/08/18 15:51:04 [debug] 16361#0: *1 http script regex: "(.*)//+(.*)"
    2014/08/18 15:51:04 [notice] 16361#0: *1 "(.*)//+(.*)" does not match "/contact-us/", client: 59.167.230.186, server: *.domain.edu, request: "GET //////contact-us//// HTTP/1.1", host:
    "test.domain.edu"


    I found this worked for me:



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


    Ref: http://rosslawley.co.uk/archive/old/2010/01/10/nginx-how-to-url-cleaning-removing/






    share|improve this answer


























    • Reference link is what you need to check. Correct solution is in there. I will try to edit the answer.

      – Anup Nair
      Apr 30 '18 at 7:36














    2












    2








    2







    I found kwo's response to not work. Looking at my debug log, this is what happens:



    2014/08/18 15:51:04 [debug] 16361#0: *1 http script regex: "(.*)//+(.*)"
    2014/08/18 15:51:04 [notice] 16361#0: *1 "(.*)//+(.*)" does not match "/contact-us/", client: 59.167.230.186, server: *.domain.edu, request: "GET //////contact-us//// HTTP/1.1", host:
    "test.domain.edu"


    I found this worked for me:



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


    Ref: http://rosslawley.co.uk/archive/old/2010/01/10/nginx-how-to-url-cleaning-removing/






    share|improve this answer















    I found kwo's response to not work. Looking at my debug log, this is what happens:



    2014/08/18 15:51:04 [debug] 16361#0: *1 http script regex: "(.*)//+(.*)"
    2014/08/18 15:51:04 [notice] 16361#0: *1 "(.*)//+(.*)" does not match "/contact-us/", client: 59.167.230.186, server: *.domain.edu, request: "GET //////contact-us//// HTTP/1.1", host:
    "test.domain.edu"


    I found this worked for me:



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


    Ref: http://rosslawley.co.uk/archive/old/2010/01/10/nginx-how-to-url-cleaning-removing/







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 25 at 11:40









    JakeGould

    32k1098140




    32k1098140










    answered Aug 18 '14 at 5:56









    DaveQBDaveQB

    213




    213













    • Reference link is what you need to check. Correct solution is in there. I will try to edit the answer.

      – Anup Nair
      Apr 30 '18 at 7:36



















    • Reference link is what you need to check. Correct solution is in there. I will try to edit the answer.

      – Anup Nair
      Apr 30 '18 at 7:36

















    Reference link is what you need to check. Correct solution is in there. I will try to edit the answer.

    – Anup Nair
    Apr 30 '18 at 7:36





    Reference link is what you need to check. Correct solution is in there. I will try to edit the answer.

    – Anup Nair
    Apr 30 '18 at 7:36











    0














    Try this:



    merge_slashes off;
    rewrite (.*)//+(.*) $1/$2 permanent;


    There might be multiple redirects for slashes > 3 or multiple groups of slashes.






    share|improve this answer
























    • 'merge_slashes off' made no difference and resulted in no change.

      – Anup Nair
      Apr 30 '18 at 7:34
















    0














    Try this:



    merge_slashes off;
    rewrite (.*)//+(.*) $1/$2 permanent;


    There might be multiple redirects for slashes > 3 or multiple groups of slashes.






    share|improve this answer
























    • 'merge_slashes off' made no difference and resulted in no change.

      – Anup Nair
      Apr 30 '18 at 7:34














    0












    0








    0







    Try this:



    merge_slashes off;
    rewrite (.*)//+(.*) $1/$2 permanent;


    There might be multiple redirects for slashes > 3 or multiple groups of slashes.






    share|improve this answer













    Try this:



    merge_slashes off;
    rewrite (.*)//+(.*) $1/$2 permanent;


    There might be multiple redirects for slashes > 3 or multiple groups of slashes.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 13 '13 at 18:06









    kwokwo

    1093




    1093













    • 'merge_slashes off' made no difference and resulted in no change.

      – Anup Nair
      Apr 30 '18 at 7:34



















    • 'merge_slashes off' made no difference and resulted in no change.

      – Anup Nair
      Apr 30 '18 at 7:34

















    'merge_slashes off' made no difference and resulted in no change.

    – Anup Nair
    Apr 30 '18 at 7:34





    'merge_slashes off' made no difference and resulted in no change.

    – Anup Nair
    Apr 30 '18 at 7:34











    -1














    URL example.com//dir1////dir2///dir3 and more
    Try this it's working for me



    merge_slashes off;
    location ~ ^(.*?)//+(.*?)$ {
    return 301 $1/$2;
    }






    share|improve this answer






























      -1














      URL example.com//dir1////dir2///dir3 and more
      Try this it's working for me



      merge_slashes off;
      location ~ ^(.*?)//+(.*?)$ {
      return 301 $1/$2;
      }






      share|improve this answer




























        -1












        -1








        -1







        URL example.com//dir1////dir2///dir3 and more
        Try this it's working for me



        merge_slashes off;
        location ~ ^(.*?)//+(.*?)$ {
        return 301 $1/$2;
        }






        share|improve this answer















        URL example.com//dir1////dir2///dir3 and more
        Try this it's working for me



        merge_slashes off;
        location ~ ^(.*?)//+(.*?)$ {
        return 301 $1/$2;
        }







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 28 at 5:38

























        answered Jan 25 at 9:10









        SoniNowSoniNow

        11




        11

















            protected by JakeGould Jan 25 at 11:40



            Thank you for your interest in this question.
            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



            Would you like to answer one of these unanswered questions instead?



            Popular posts from this blog

            Plaza Victoria

            Puebla de Zaragoza

            Musa