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

July 20, 2016 291 views

I have!
and I want to rewrite to

3 Answers

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;
  rewrite ^/(.*)/$ /$1 permanent;

To remove the trailing slash on ONE URL:

server {
  listen 80;
  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.

  • @paul.stocks - just edited my answer, realized i didnt understand your question correctly the first time.

@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

  • Which javascript framework is your site using?

    The #!... is called a hashbang and is used by javascript frameworks to handle URL-routing on the client-side. The server never even knows about everything after the # symbol in the URL.

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!

Have another answer? Share your knowledge.