How to Rewrite URL in Nginx


how to rewrite url in nginx

When you are running a website, you may need to delete or move certain web pages and folders. In such cases, it’s essential to redirect old URLs to new ones so that both search engines and site visitors know that your content has moved. Here’s how to rewrite URL in Nginx.

 

How to Rewrite URL in Nginx

Nginx allows you to redirect URLs using return and redirect directive.

 

Return Directive

You can place the return directive in server or location block, like so


return 301 https://example.com$request_uri;

 

Here’s an example of redirecting your pages to a new domain (old-name.com to new-name.com)


server {
 listen 80;
 listen 443 ssl;
 server_name www.old-name.com;
 return 301 $scheme://www.new-name.com$request_uri;
}

 

Similarly, if you want to redirect all http requests to https requests, here’s an example. Replace example.com with your domain name


server {
 listen 80;
 listen [::]:80;
 hostname example.com www.example.com;
 return 301 https://example.com$request_uri;
}

 

If you want a temporary redirect, use 302 instead of 301, in the above code. You can also return other return codes such as 1xx, 2xx, 4xx, 5xx

 

Rewrite Directive

Rewrite directive is meant for complex URL redirection which requires parsing the URL. Rewrite allows you to test URLs for patterns using regular expressions. Like return directive, it can be placed in server or location blocks. However, rewrite will redirect only those URLs that match its regular expression, unlike return directive that redirects all requests in a server/location context.

 

Here’s the syntax for rewrite directive


rewrite regex URL [flag];

 

Here’s an example,


rewrite ^/foo/(bar)/(.*)$ https://$server_name/$1/$2 permanent;

Replace permanent with redirect if you want temporary redirection.

 

Unlike return directive, rewrite can return only 301 or 302 codes. It cannot return 4xx codes like 404.

 

Here’s an example of rewrite directive. Here it redirects only those URLs that begin with /download and contain /media. It rewrites the ‘media’ part to ‘mp3’.


server {
 # ...
 rewrite ^(/download/.*)/media/(\w+)\.?.*$ $1/mp3/$2.mp3 last;
 return 403;
 # ...
}

In this case, /download/cdn/media/file1 become /download/cdn-west/mp3/file1

 

In the above examples, I have used some variable names that are pre-defined by Nginx. Here are the important ones for quick reference

  • $scheme – URL scheme in request E.g. “http” or “https”
  • $host – Hostname provided in requested URL
  • $server_name – Hostname declaration in your config for the server block that matches the request.
  • $request_uri – full URL, with arguments
  • $request_filename – file path of request

 

That’s it! Now you know how to rewrite URL in Nginx. Luckily, there is no installation of configuration required to enable URL rewrites. You can simply add them in server/location blocks of your Nginx config file.

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!