How nginx rewrite implements URL jump

PHPz
Release: 2023-06-03 11:55:28
forward
1148 people have browsed it

url跳转

这里说的url跳转就是用户在访问一个url时将其跳转到另一个url上。

常见的应用场景是让多个域名跳转到同一个url上,(例如让旧域名跳转到新域名上)

将静态文件请求跳转到cdn上等

根据用户设备跳转到不同站点(pc版,wap版)等。

url跳转可以通过js在页面上设置的window.location实现

也可以通过php设置header来实现

当然也可以用nginx 的 rewrite功能实现

nginx rewrite模块

rewrite 是 nginx的静态重写模块

基本用法是 rewrite patten replace flag

patten是正则表达式,与patten匹配的url会被改写为replace,flag可选

例如将旧域名跳转到新域名上

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

跳转到新域名上时保留路径

server { listen 80; server_name www.old.com; rewrite "^/(.*)$" http://www.new.com/$1; } rewrite与location配合实现图片文件跳转到cdn location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; rewrite "^/uploadfile\/(.*)$" http://static.xxx.com/uploadfile/$1; }
Copy after login

rewrite后面可以加flag,flag标记有:

last相当于apache里的[l]标记,表示完成rewrite

break终止匹配, 不再匹配后面的规则

redirect返回302临时重定向 地址栏会显示跳转后的地址

permanent返回301永久重定向 地址栏会显示跳转后的地址

The above is the detailed content of How nginx rewrite implements URL jump. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!