Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Our site is based on Angular which makes it almost completely JavaScript based, therefore we need to serve static HTML snapshots to the Googlebot in order for it to crawl us. At the moment, we have this implementation in place:

location / {
    # Rewrite rules for the Google and Bing bots and other crawlers.
    # Serves static HTML from /snapshots when a URL is requested
    if ($http_user_agent ~ (Googlebot|bingbot|Screaming)) {
        rewrite ^/(.*)$ /snapshots/$1.html break;

This works in most cases, however if Google was to request a URL such as: it will be rewritten into: which obviously returns a 404. I need to change the config to remove forward slashes at the end of a URL, to make sure this is returned:

How can this be achieved from within the nginx configuration? We're seeing hundreds of 404 errors in Webmaster Tools because of this.


share|improve this question

If you change your rewrite to

rewrite ^/(.*)/$ /snapshots/$1.html break;
rewrite ^/(.*)$ /snapshots/$1.html break;

then the first line will only match lines ending in a slash, and $1 will contain the full path minus the leading and trailing slashes. The second will catch the rest of the cases (that are working now).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.