تحميل

How to Create Nginx Redirect (Temporary and Permanent)

Updated at
22/08/2023
Views
7544

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

لماذا تختارنا؟

أفضل الأسعار, تخزين SSD سريع و دعم فني
24/7 حقيقي

control panel icon
cPanel

إدارة موقعك من خلال CPanel بأحدث إصدار

security icon
سريع وموثوق وآمن

ضمان وقت تشغيل بنسبة 99.8٪.
لذا ركز على عملك!

support icon
دعم 24/7/365

ليلاً أو نهارًا ، مطر أو شمس ،
فريقنا هنا من أجلك!

cloud backup icon
نسخ احتياطي يومي

نسخ احتياطي يومي وأسبوعي.
ملفاتك آمنة!