How to do a 301 redirect with NGINX and Virtualmin

How to redirect pages to another location using NGINX

There are a few different ways to redirect pages from one location to another with NGINX. In this article we document some possible ways to do it, and we also include screenshots and references to Virtualmin if this is your website hosting platform of choice.

In all instances the changes need to happen in the virtual host nginx configuration file unlike Apache where you can just add a .htaccess file. NGINX doesn’t support .htaccess files.

Please also remember that redirects may be order specific. This is particularly important if you’re also redirecting individual pages, in which case, the redirects has to be at the start of your configuration file.

A typical location for a NGINX configration file is:


Edit the requisite file and add any of the methods to the site’s configuration that you want to redirect.

Please note that if you have WordPress hosting, you can probably avoid making any of these changes by just specifying https for all your URLs, especially in the Settings->General section. See `WordPress Address (URL)` and `Site Address (URL)`

Method 1 – if scheme check

A nice and simple way to do redirect is to add the following at the top of your server block:

if ($scheme = http) {
   return 301 https://$host$request_uri;

Method 2 – additional server block

Another way of doing this check if the following additional server block, added above the existing server block:

server {
        server_name domain.com www.domain.com;
        listen ipaddress:80;
        rewrite ^/(.*) https://domain.com/$1 permanent;

Method 3 – individual page redirect

Below is an example of a page direct, from one part of the site, to another part of the site. Make specific notes here that the redirect has been put right at the top of the NGINX configuration. In our experience, it won’t work if you for example use the Virtualmin UI as this interace puts it at the bottom of  the configuration file.

server {
   rewrite /page-a/ /page-b/ permanent;

Method 4 – Complex redirection for Multi Site WordPress Hosting

If you are hosting multiple WordPress sites, you might need a more complex redirection that splits off the WWW redirection from the non-WWW redirection. Below is an example of NGINX redirection for three sub sites, which are multi site WordPress hosting for the main site.


  • Four server blocks
  • For each subsite block, be sure to specify the ssl.combined and ssl.key locations
  • In the main server block, note that all sites appear with www, whereas in the 3 x subsite server blocks, the sites appear without the www
  • In main server block,  if ($scheme = http) redirection.
server {
                server_name subsite1.com;
                listen a.b.c.d;
                rewrite ^/(.*) https://www.subsite1.com/$1 permanent;
                listen a.b.c.d:443 ssl;
                ssl_certificate /home/subsite1/ssl.combined;
                ssl_certificate_key /home/subsite1/ssl.key;
        server {
                server_name subsite2.com;
                listen a.b.c.d;
                rewrite ^/(.*) https://www.subsite2.com/$1 permanent;
                listen a.b.c.d:443 ssl;
                ssl_certificate /home/subsite2/ssl.combined;
                ssl_certificate_key /home/subsite2/ssl.key;
        server {
                server_name subsite3.com;
                listen a.b.c.d;
                rewrite ^/(.*) https://www.subsite3.com/$1 permanent;
                listen a.b.c.d:443 ssl;
                ssl_certificate /home/subsite3/ssl.combined;
                ssl_certificate_key /home/subsite3/ssl.key;
        server {
                if ($scheme = http) {
                    return 301 https://$host$request_uri;
                server_name mainsite.com www.mainsite.com www.subsite1.com www.subsite2.com www.subsite3.com;
                listen a.b.c.d;
                root /home/mainsite/public_html;

Virtualmin Specific Information for NGINX redirection

Virtualmin has two available places where NGINX direction can be affected as indicated by the screenshots below.

If you want to deploy this configuration to *all* new sites, add an “Additional Nginx directive”:

The default setting for  SSL redirection can also be controlled like below:

Page Specific UI

In Virtualmin, under Services->Configure Nginx Website, you have the following UI:

This will add redirect to the sites NGINX configuration file, but right at the end. If your redirect do not work, move them to the front.

Friendly URL Redirection

If none of your hyperlinks WordPress or a Laravel Applicaiton works, but the home page loads, then you might be missing the following redirection:

location / {
   try_files $uri $uri/ /index.php?$query_string;


Redirection can get a bit tricky at times. If you need any assistance, please contact us.



If you want to do this for Apache and use the .htaccess method, follow this link:

How to redirect all URLs from one location to another location using the .htaccess file


How to password protect NGINX directories

Share this article

Share on facebook
Share on twitter
Share on linkedin
Scroll to Top