How to redirect URL using nginx rewrite

How to redirect URL using nginx rewrite

I often need to change nginx configuration at work recently, and learned how to use rewrite in nginx

URL redirection

The URL redirection mentioned here means that when a user visits a URL, it is redirected to another URL.

A common application scenario is to redirect multiple domain names to the same URL (for example, redirect an old domain name to a new domain name).

Redirect static file requests to CDN, etc.

Jump to different sites (PC version, WAP version), etc. according to the user's device.

URL redirection can be achieved by setting window.location on the page through js

It can also be achieved by setting the header in PHP

Of course, you can also use nginx's rewrite function to achieve

nginx rewrite module

rewrite is the static rewrite module of nginx

The basic usage is rewrite pattern replace flag

patten is a regular expression. URLs matching patten will be rewritten as replace. flag is optional.

For example, redirect the old domain name to the new domain name

server
{
 listen 80;
 server_name www.old.com;
 rewrite ".*" http://www.new.com;
}

Keep the path when redirecting to a new domain

server
{
 listen 80;
 server_name www.old.com;
 rewrite "^/(.*)$" http://www.new.com/$1;
}
Rewrite and location work together to jump image files to cdn
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
 expires 30d;
 rewrite "^/uploadfile\/(.*)$" http://static.XXX.com/uploadfile/$1;
}

You can add a flag after rewrite . The flag tags are:

last is equivalent to the [L] mark in Apache, indicating that rewrite is completed

break terminates the match and no longer matches the following rules

redirect returns 302 temporary redirection. The address bar will display the redirected address.

Permanent return 301 permanent redirection address bar will display the address after the jump

The above method of using nginx rewrite to achieve URL redirection is all I have to share with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of Nginx's rewrite module
  • Nginx URL rewriting mechanism principle and usage examples
  • Nginx rewrite regular matching rewriting method example
  • Detailed explanation of location and rewrite usage in nginx
  • Detailed explanation of nginx rewrite and location according to URL parameters
  • Nginx rewrite rewrite basics and examples sharing
  • Nginx Rewrite rules and usage introduction and skills examples
  • nginx rewrite pseudo-static configuration parameters and usage examples
  • Analysis of nginx rewrite function usage scenarios

<<:  MySQL 5.7.23 installation and configuration graphic tutorial

>>:  JavaScript implements class lottery applet

Recommend

Interviewer asked how to achieve a fixed aspect ratio in CSS

You may not have had any relevant needs for this ...

How to check and organize website files using Dreamweaver8

What is the purpose of creating your own website u...

Implementation of Vue counter

Table of contents 1. Implementation of counter 2....

Several common methods for passing additional parameters when submitting a form

When submitting a form, you may encounter situatio...

CSS3 uses var() and calc() functions to achieve animation effects

Preview knowledge points. Animation Frames Backgr...

How to smoothly go online after MySQL table partitioning

Table of contents Purpose of the table For exampl...

Generate OpenSSL certificates in Linux environment

1. Environment: CentOS7, Openssl1.1.1k. 2. Concep...

How to install rabbitmq-server using yum on centos

Socat needs to be installed before installing rab...

Detailed explanation of common methods of JavaScript String

Table of contents 1. charAt grammar parameter ind...

JavaScript realizes the generation and verification of random codes

The generation and verification of random codes i...

How to solve the problem of margin overlap

1. First, you need to know what will trigger the v...

Detailed explanation of JavaScript Proxy object

Table of contents 1. What is Proxy? 2. How to use...

How to use the EXPLAIN command in SQL

In daily work, we sometimes run slow queries to r...

Restart all stopped Docker containers with one command

Restart all stopped Docker containers with one co...

MySQL trigger principle and usage example analysis

This article uses examples to explain the princip...