loading

How to Create Nginx Redirect (Temporary and Permanent)

Updated at
22/08/2023
Views
7712

Creating redirects in Nginx can be useful when you need to redirect traffic from one URL to another. There are two types of redirects you can create in Nginx: temporary (HTTP 302) and permanent (HTTP 301).

Here are the steps to create temporary and permanent redirects in Nginx:

1. Edit the Nginx configuration file for your website or application using a text editor. This file is typically located at "/etc/nginx/conf.d/yourwebsite.conf".

```
sudo nano /etc/nginx/conf.d/yourwebsite.conf
```

2. To create a temporary redirect, add the following lines to the configuration file:

```
location /old-url {
return 302 /new-url;
}
```

Replace "/old-url" with the URL you want to redirect, and "/new-url" with the new URL you want to redirect to.

3. To create a permanent redirect, add the following lines to the configuration file:

```
location /old-url {
return 301 /new-url;
}
```

Replace "/old-url" with the URL you want to redirect, and "/new-url" with the new URL you want to redirect to.

4. Save the configuration file and exit the text editor.

5. Test the redirect by accessing the old URL in a web browser. You should be automatically redirected to the new URL.

Note: If the redirect is not working, you may need to clear your web browser's cache and cookies.

That's it! You have successfully created temporary and permanent redirects in Nginx.

YottaSrc footer

why choose us?

Better Pricing, Fast SSD Storage and Real 24/7
Technical Support.

control panel icon
cPanel

Managing Your Site Through CPanel With The Latest Version

security icon
FAST, RELIABLE & SECURE

99.8% Uptime Guarantee.
So Focus On Your Work!

support icon
24/7/365 Support

Day or night, rain or shine,
our team is here for you!

cloud backup icon
Daily Backup

Daily & Weekly Backup.
Your Files Are Safe!