---
title: "How to Create NGINX Redirects"
description: "NGINX can be used as a web server (in lieu of Apache) or proxy server. For those using it as a web server, creating NGINX redirects requires editing server configuration files. This is different from..."
url: https://www.inmotionhosting.com/support/server/nginx/nginx-redirect/
date: 2021-04-01
modified: 2025-11-07
author: "Carrie Smaha"
categories: ["NGINX"]
type: post
lang: en
---

# How to Create NGINX Redirects

![NGINX Redirects on NGINX Web Server](https://www.inmotionhosting.com/support/wp-content/uploads/2021/03/canva-nginx-redirects-1024x538.jpg)

[**NGINX**](https://www.inmotionhosting.com/support/server/nginx/) can be used as a [web server](https://www.inmotionhosting.com/support/server/server-usage/nginx-basics/) (in lieu of Apache) or [proxy server](https://www.inmotionhosting.com/support/server/nginx/nginx-reverse-proxy-edition/). For those using it as a web server, creating NGINX redirects requires editing server configuration files. This is different from Apache which also checks [.htaccess](https://www.inmotionhosting.com/support/website/setting-up-a-301-permanent-redirect-via-htaccess/) files during web server requests. [NGINX doesn’t use .htaccess](https://www.nginx.com/resources/wiki/start/topics/examples/likeapache-htaccess/). Need to know how to redirect HTTP to HTTPS in NGINX? Don’t worry. It’s a simple process, as easy as [hiding your NGINX server version](https://www.inmotionhosting.com/support/server/nginx/hide-nginx-server-version/).

## Create NGINX Redirects

[SSH into your NGINX web server](https://www.inmotionhosting.com/support/server/ssh/how-to-login-ssh/) as user root. Edit your NGINX configuration file using [Nano](https://www.inmotionhosting.com/support/server/ssh/how-to-edit-files-using-nano/), [Vim](https://www.inmotionhosting.com/support/server/ssh/vim/), [Emacs](https://www.inmotionhosting.com/support/edu/emacs/), etc. We’ll be editing domain configuration files in the `sites-enabled` directory as it is the easiest method to create NGINX redirects. The file may state *default* or take the name of the domain.

nano /etc/nginx/sites-enabled/default
vim /etc/nginx/sites-enabled/example.com

```
nano /etc/nginx/sites-enabled/default
vim /etc/nginx/sites-enabled/example.com
```

Ignoring the comment lines, the default file will resemble the code block below:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;
        server_name _;
        location / {
                try_files $uri $uri/ =404;
        }
}

```
server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;
        server_name _;
        location / {
                try_files $uri $uri/ =404;
        }
}
```

Most changes below can all be done within existing code. Just remember to restart NGINX after making changes for them to take effect.

systemctl restart nginx

```
systemctl restart nginx
```

To save time troubleshooting, negate caching issues by testing changes in a [private browsing session](https://www.inmotionhosting.com/support/resources/how-to-start-a-private-browsing-session/).

### Redirect HTTP to HTTPS

To force encrypted connections, add the following to a new line under `server_name _;`:

return 301 https://$host$request_uri;

```
return 301 https://$host$request_uri;
```

You can install a free domain-validated SSL certificate with [Certbot](https://www.inmotionhosting.com/support/website/ssl/lets-encrypt-ssl-ubuntu-with-certbot/).

### Redirect to Force www

Deciding to explicitly use “www” or not for your website improves [search engine optimization (SEO)](https://www.inmotionhosting.com/support/website/seo/seo-getting-started-guide/). First, add the following above the existing `server {` line:

server {
        listen       80;
        server_name  www.domain.com;
}

```
server {
        listen       80;
        server_name  www.domain.com;
}
```

If you want to force HTTPS, add this line above the closing `}` bracket:

return 301 https://www.domain.com$request_uri;

```
return 301 https://www.domain.com$request_uri;
```

Finally, add the following to a new line under `server_name _;`:

return 301 http://www.domain.com$request_uri;

```
return 301 http://www.domain.com$request_uri;
```

### Redirect a Domain to Another Domain

This is useful when migrating your website to a newer domain or subdomain (e.g. /blog or /forum). Add the following to a new line under `server_name _;`:

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

```
return 301 https://newdomain.com$request_uri;
```

### Permanent Redirect for a Single File

To redirect a single page or file URL, add the following to a new line under `server_name _;`:

rewrite ^/page1.html$ /page2.html permanent;

```
rewrite ^/page1.html$ /page2.html permanent;
```
