title: NGINX Rewrite to strip a URL Segment
body: <p>I have moved my site to a new server running nginx from apache.</p>
  
  <p>The old site structure was: </p>
  
  <p>example.com/NEW/</p>
  
  <p>The new structure:</p>
  
  <p>example.com/</p>
  
  <p>I have a few applications that still reference uploads/files like:</p>
  
  <p>example.com/NEW/assets/images/7871862618261826.jpg</p>
  
  <p>I need to rewrite all the requests that include the /NEW/ parameter.</p>
  
  <p>eg: </p>
  
  <p>example.com/NEW/assets/images/7871862618261826.jpg</p>
  
  <p>should be routed to:</p>
  
  <p>example.com/assets/images/7871862618261826.jpg</p>
  
  <p>My Current Nginx config looks like:</p>
  
  <pre><code>server {
      listen 80;
      server_name www.domain.com domain.com;
      return 301  https://www.domain.com$request_uri;
  }
  </code></pre>
  
  <p>server {</p>
  
  <pre><code>listen  443;
  if ($http_host = domain.com) {
      rewrite  (.*)  https://www.domain.com$1;
  }
  
  location /NEW/ {
  
       rewrite ^/NEW(.*)$ $1 last;
       #return 405;
       #return 301  https://www.domain.com$request_uri;
  }
  
  
  server_name domain.com www.domain.com;
  
  access_log /var/log/nginx/domaincom.access.log;
  error_log /var/log/nginx/domaincom.error.log;
  
  root /var/www/domain.com/public_html/;
  index index.html index.htm index.php;
  
   #set default location
   location / {
  
          proxy_set_header X-Real-IP  $remote_addr;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_set_header Host $host;
          proxy_pass http://127.0.0.1:8080;
  
   }
  
  }
  </code></pre>
  
  <p>How can I achieve the rewrite?</p>