Question

How do I get nginx to remove trailing '/' and characters?

I have http://mydomain.com/page/#! and I want to rewrite to http://mydomain.com/page


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

We are not generating these. I believe they are coming from Bing (though I’m not positive, SEO guy will be in later this am). Our backend (or frontend) didn’t make them. We are just trying to service SEO bot traffic. This particular app is written in Meteor, though I don’t believe that has any bearing on the genesis of the routes. There was an old website (same domain) that did generate a bunch of routes we handle with rewrites … and maybe this is coming from some old cache … Thanks a bunch for your original post it was most helpful in understanding NGIX rewrites as the docs are painful to read!

@aha I currently have the redirect for a trailing ‘/’. What I need is to be able to remove it AND 'additional characters’. I have ‘/#!’ and its causing SEO issues

EDIT: I realized my initial answer on hashbang didn’t answer your trailing slash question.

To remove the trailing slash on ALL URLs: Add a rewrite directive to the server block of your nginx config file that looks for URL’s ending with a trailing slash and permanently redirects to the same URL without the slash:

server {
  listen 80;
  server_name mydomain.com;
  rewrite ^/(.*)/$ /$1 permanent;
}

To remove the trailing slash on ONE URL:

server {
  listen 80;
  server_name mydomain.com;
  rewrite ^/page/$ /page permanent;
}

Keep in mind nginx never actually sees the #! or “hashbang” part of the URL, and it may appear even when you do the above redirects if it is set via javascript.