7644

remove trailing slash in nginx with some certain cases ignored

Question:

I have the following rewrite rule in my nginx:

rewrite ^/(.*)/$ /$1 permanent;

to remove trailing slash at the end of any URL. However I wanted to make an exception such that when the URL is /register/ I don't want this rule to be applied. How do I put that into the regex?

Answer1:

You can use a <strong>Negative Lookahead</strong>.

^/(?!register)(.*)/$

If you don't want register anywhere between such as /exampleregister/, use the following.

^/(?!.*register)(.*)/$

Recommend

  • Stale-while-revalidate cache replacement from Varnish
  • make m4 see macro when macro ends with same character as string following macro
  • Deployment Rails app for Docker
  • How to exit “docker run” containers once the script those containers execute calls exit()
  • Can I use preg_replace to strip the trailing \\n in multiline mode?
  • auto layout leading or trainling -16
  • Custom URI routing by query string with CodeIgniter?
  • How to create a dollar amount input field in an HTML form?
  • Why does my regular expression fail with certain substitutions?
  • Why does _GET in PHP wrongly decodes slash?
  • Can't configure nginx as a proxy for tomcat with context path
  • Crafting a LINQ based solution to determine if a set of predicates are satisfied for a pair of colle
  • Persistent stty Settings?
  • Multiple flexboxes with margin-right, except the last one in the row? Without JS?
  • UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer
  • C++/CLI Thread synchronization including managed and unmanaged code
  • help('modules') crashing? Not sure how to fix
  • Zeromq with python hangs if connecting to invalid socket
  • Refactoring advice: maps to POJOs
  • Haskell: function composition with anonymous/lambda function
  • Regex for URL rewrite with optional query string parameters
  • F#: In which memory area is the continuation stored: stack or heap?
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • Universal Image Loader reuse images
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • Create Instant using a negative year
  • Apache RewriteRule redirection with url encoded
  • Zurb Foundation _global.scss meta styles for js?
  • Unable to decode certificate at client new X509Certificate2()
  • Alternative to overridePendingTransition() - Android
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • Use of this Javascript
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • Circular dependency while pushing http interceptor
  • How to disable jQuery.jplayer autoplay?
  • AngularJs get employee from factory
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery