Nginx rewrite rules with regex applied to one subdirectory only

nginxregexrewriteurl

I've been frustrated for days trying to figure this out, so any advice/help would be greatly appreciated!

I've recently added a section called /links to a site at somehost.com but can't seem to get the rewrite rules going for this directory (see below). All other paths of the domain are working fine (say for example somehost.com/browse/video/whatever redirects to somehost.com/by/whatever/ fine) and all other rewrite rules seem to still be working great, it's just the newly added location /links/ section to my conf where things fall apart.

The following conf fails on line 27 — The error I get when using service nginx restart is: directive "rewrite" is not terminated by ";" but I can see that the end line terminates with a ; so I'm not sure what's wrong. Perhaps something with the regex?

My nginx conf is below. Any ideas on what to alter to get URLs like somehost.com/links/page/3 or somehost.com/links/tag/sometopic/page/2 working?

Thanks!

server {
  server_name somehost.com;
  listen 80;
  root /var/www/somehost.com;
  include /etc/nginx/php.conf;

  location / {
    index index.php;
    access_log /var/log/nginx/somehost.com.log;

    #handle old variable redirects first, before dealing with current permalinks
    rewrite ^/?browse/video/(.*) /by/$1 permanent;

    #set a 30-day expires header on static files for cache
    if (-f $request_filename) { expires 30d; break; }    

    #do current permalinks with args
    try_files $uri $uri/ /index.php?$args;
   }

  #handle static redirects from old URLs and rewrite
  location /browse/title { rewrite ^(.*)$ /browse/?orderby=title redirect; }
  location /browse/featured { rewrite ^(.*)$ /by/featured/ permanent; }

  #handle /links permalinks etc
  location /links/ {
    rewrite "tag/(.+)/page/([0-9]{1,})/s/([^/]+)(/)?"$ /?tag=$1&page=$2&s=$3 break;
    rewrite "tag/(.+)/page/([0-9]{1,})(/)?" /?tag=$1&page=$2 break;
    rewrite tag/(.+)/s/([^/]+)(/)? /?tag=$1&s=$2 break;
    rewrite tag/([^/]+)(/)? /?tag=$1 break;
    rewrite rss/([^/]+) /rss.php?tag=$1 break;
    rewrite rss /rss.php break;
    rewrite "page/([0-9]{1,})/s/([^/]+)" /index.php?page=$1&s=$2 break;
    rewrite "page/([0-9]{1,})" /index.php?page=$1 break;
    rewrite go/([0-9]+) /ir.php?id=$1 break;
  }

}

Best Answer

The dollar is outside of the quotes in the first regular expression of your rewrite.