Symphony CMS nginx rewrite rules

If you’re looking to use Symphony CMS on your nginx powered web server then the default Apache .htaccess rewrites will need to translated to nginx’s syntax.

I recently wrote about Symphony CMS and have since had a few people asking about the specific nginx rewrite rules I use, so I thought I’d share them here.

My rules are taken from Nick Dunn’s Gist posting I found on the Symphony forums, so all credit goes to Nick and the guys over there.

These rules need to be included in your nginx config file often found in /etc/nginx/sites-available/example.com, and you’ll need to adjust ‘example.com’ to your URL and the server root settings as appropriate.

Symphony CMS nginx rewrite rules

These are the rules you’ll need to for everything to work:

location / {
  index index.php;

  # serve static files directly
  if (-f $request_filename) {
    access_log        off;
    expires           30d;
    break;
  }

  ### BACKEND
  if ($request_filename ~ /symphony/) {
  rewrite ^/symphony/?$ /index.php?mode=administration&$query_string last;
  rewrite ^/symphony(/(.*/?))?$ /index.php?symphony-page=$1&mode=administration&$query_string last;
  }

  ### IMAGE RULES
  rewrite ^/image/(.+\.(jpg|gif|jpeg|png|bmp|JPG|GIF|JPEG|PNG|BMP))$ /extensions/jit_image_manipulation/lib/image.php?param=$1 last;

  ## Add trailing slash
  rewrite ^(.*[^/])$ $1/ permanent;

  ### MAIN REWRITE - This will ignore directories
  if (!-d $request_filename) {
  rewrite ^/(.*)$ /index.php?symphony-page=$1 last;
  }
}

Complete nginx config file

If you need a full working example of an nginx server config including PHP, Symphony CMS’s rewrites and a simple http://example.com to http://www.example.com rewrite, you can use the following:

# rewrite from example.com to www.example.com
server {
    listen 80;
    server_name example.com;
    rewrite ^(.+?)/?$ http://www.example.com$1 permanent;
}

server {
    listen 80;
    server_name www.example.com;
    access_log /var/www/example.com/logs/access.log;
    error_log /var/www/example.com/logs/error.log;
    root /var/www/example.com/public_html;

    location / {
        index index.php;

        # serve static files directly
        if (-f $request_filename) {
          access_log        off;
          expires           30d;
          break;
        }

        ### BACKEND
        if ($request_filename ~ /symphony/) {
          rewrite ^/symphony/?$ /index.php?mode=administration&$query_string last;
          rewrite ^/symphony(/(.*/?))?$ /index.php?symphony-page=$1&mode=administration&$query_string last;
        }

        ### IMAGE RULES
        rewrite ^/image/(.+\.(jpg|gif|jpeg|png|bmp|JPG|GIF|JPEG|PNG|BMP))$ /extensions/jit_image_manipulation/lib/image.php?param=$1 last;

        ## Add trailing slash
        rewrite ^(.*[^/])$ $1/ permanent;

        ### MAIN REWRITE - This will ignore directories
        if (!-d $request_filename) {
            rewrite ^/(.*)$ /index.php?symphony-page=$1 last;
        }

    }

    location ~ \.php {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/example.com/public_html$fastcgi_script_name;
    }
}

By Mark Hesketh

I'm a PHP Developer based in UK, building modern PHP web applications and writing about everything I learn.