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

Top 10 useful and important open source tools in 2019

In Black Duck's 2017 open source survey, 77% ...

Notes on using the blockquote tag

<br />Semanticization cannot be explained in...

Installation and configuration of MySQL 5.7.17 free installation version

MYSQL version: MySQL Community Server 5.7.17, ins...

Implementing shopping cart function based on vuex

This article example shares the specific code of ...

Essential conditional query statements for MySQL database

Table of contents 1. Basic grammar 2. Filter by c...

A detailed introduction to the basics of Linux scripting

Table of contents 1. Script vim environment 2. Ho...

Vue3 slot usage summary

Table of contents 1. Introduction to v-slot 2. An...

How to import and export Docker images

This article introduces the import and export of ...

Getting Started Tutorial on Using TS (TypeScript) in Vue Project

Table of contents 1. Introducing Typescript 2. Co...

CSS3 to achieve floating cloud animation

Operation effect html <head> <meta chars...

How to encapsulate the table component of Vue Element

When encapsulating Vue components, I will still u...

Detailed Analysis of Explain Execution Plan in MySQL

Preface How to write efficient SQL statements is ...

Set IE8 to use IE7 style code

<meta http-equiv="x-ua-compatible" co...