当前位置: 首页 > 编程文章 >

Nginx伪静态配置与常用Rewrite规则详解

Nginx伪静态配置与常用Rewrite规则详解

作者:许舟时间:2025-12-30 10:13:51浏览:207

Nginx通过直接在nginx.conf中编写规则实现伪静态,无需加载额外模块。可通过include指令将规则分离至独立文件以简化维护。本文整理了基于正则表达式的通用规则及针对Discuz、WordPress等常见CMS的特定配置示例,涵盖目录转换、防盗链、域名跳转等场景,帮助快速构建高效的URL重写策略。

本文目录展开

在Nginx环境中配置伪静态时,可直接在nginx.conf主配置文件中定义规则,这与Apache需要启用mod_rewrite模块才能支持伪静态的方式不同。

只需编辑nginx.conf配置文件,并在对应的server块内添加所需的伪静态规则即可生效。

server
{
listen       80;
server_name  bbs.jb51.net;
index index.html index.htm index.php;
root  /home/www/bbs;
error_page  404                                             /404.htm;       #配置404错误页面
location ~ .*.(php|php5)?$
{
#fastcgi_pass  unix:/tmp/php-cgi.sock;
fastcgi_pass  127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
#下面就是伪静态了
location /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
access_log  access_log   off;
}

虽然直接修改配置文件后重启Nginx即可使伪静态生效,但这种方式不利于后期维护。因此,建议将规则提取到外部独立文件中,例如创建名为bbs_nginx.conf的文件。

首先,在/home/www/bbs目录下新建bbs_nginx.conf文件,并将具体的伪静态规则写入其中。

ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}

接着,在主配置文件的相应位置引入该外部规则文件。

在配置中添加如下代码:include /home/www/bbs/bbs_nginx.conf;

通过上述方式,位于网站根目录下的bbs_nginx.conf文件中的伪静态规则即可被独立管理和调用。

以下是一个具体的操作实例:

1. 在使用.htaccess文件的目录下新建一个.htaccess文件,例如在Discuz论坛目录中:

执行命令 vim /var/www/html/jb51/bbs/.htaccess

2. 在该文件中输入规则,此处以添加Discuz的伪静态规则为例(仅增加Discuz的伪静态规则):

# nginx rewrite  rule
rewrite ^(.*)/archiver/((fid|tid)-[w-]+.html)$ $1/archiver/index.php?$2 last;
rewrite ^(.*)/forum-([0-9]+)-([0-9]+).html$ $1/forumdisplay.php?fid=$2&page=$3 last;
rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^(.*)/profile-(username|uid)-(.+).html$ $1/viewpro.php?$2=$3 last;
rewrite ^(.*)/space-(username|uid)-(.+).html$ $1/space.php?$2=$3 last;
rewrite ^(.*)/tag-(.+).html$ $1/tag.php?name=$2 last;
# end nginx rewrite rule

保存并退出编辑器。

3. 接下来修改Nginx的主配置文件:

执行命令 vim /etc/nginx/nginx.conf

4. 在需要应用伪静态的虚拟主机server{}块中,引入刚才创建的.htaccess文件:

添加代码 include /var/www/html/jb51/bbs/.htaccess; (注意:请将路径替换为.htaccess文件的实际位置)

保存并退出编辑器。

5. 最后重新加载Nginx配置文件以使更改生效:

执行命令 /etc/init.d/nginx reload

Nginx常用Rewrite伪静态规则

伪静态规则是进行URL重写的关键参数,深入理解这些规则有助于快速编写出最优的伪静态代码。以下整理了一些常用示例供参考。

本日志内容来源于互联网及日常使用经验,整理如下以便日后查阅。 涉及的正则表达式匹配说明如下:

* ~ 为区分大小写匹配
* ~* 为不区分大小写匹配
* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配
文件及目录匹配,其中:
* -f和!-f用来判断是否存在文件
* -d和!-d用来判断是否存在目录
* -e和!-e用来判断是否存在文件或目录
* -x和!-x用来判断文件是否可执行
flag标记有:
* last 相当于Apache里的[L]标记,表示完成rewrite
* break 终止匹配, 不再匹配后面的规则
* redirect 返回302临时重定向 地址栏会显示跳转后的地址
* permanent 返回301永久重定向 地址栏会显示跳转后的地址

可用的全局变量包括以下内容,可用于条件判断(列表待补全):

$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri

结合QeePHP框架的使用示例:

if (!-d $request_filename) {
rewrite ^/([a-z-A-Z]+)/([a-z-A-Z]+)/?(.*)$ /index.php?namespace=user&controller=$1&action=$2&$3 last;
rewrite ^/([a-z-A-Z]+)/?$ /index.php?namespace=user&controller=$1 last;
break;

多目录结构转换为参数的示例:

abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2
if ($host ~* (.*)/.domain/.com) {
set $sub_name $1;
rewrite ^/sort//(/d+)//?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}

目录结构对换的示例:

/123456/xxxx -> /xxxx?id=123456
rewrite ^/(/d+)/(.+)/ /$2?id=$1 last;
例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}

自动为目录添加“/”后缀的示例:

if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}

禁止访问.htaccess文件的示例:

location ~//.ht {
deny all;
}

禁止访问多个特定目录的示例:

location ~ ^/(cron|templates)/ {
deny all;
break;
}

禁止访问以/data开头的文件的示例:

可以禁止对/data/下多级目录下.log.txt等请求的访问;

location ~ ^/data {
deny all;
}

禁止访问单个特定目录的示例:

注意:此配置不能禁止对.log.txt的请求

location /searchword/cron/ {
deny all;
}

禁止访问单个特定文件的示例:

location ~ /data/sql/data.sql {
deny all;
}

为favicon.ico和robots.txt设置过期时间;此处设定favicon.ico为99天,robots.txt为7天,且不记录404错误日志

location ~(favicon.ico) {
log_not_found off;
expires 99d;
break;
}
location ~(robots.txt) {
log_not_found off;
expires 7d;
break;
}

设定某个特定文件的过期时间;此处设为600秒,且不记录访问日志

location ^~ /html/scripts/loadhead_1.js {
access_log off;
root /opt/lampp/htdocs/web;
expires 600;
break;
}

配置文件和目录的反盗链功能并设置过期时间:

这里的return 412 为自定义的HTTP状态码,默认为403,此举便于准确识别正确的盗链请求

“rewrite ^/ //www.jb51.net/jb51.gif;”显示一张防盗链图片
“access_log off;”不记录访问日志,减轻压力
“expires 3d”所有文件3天的浏览器缓存
location ~* ^.+/.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
valid_referers none blocked *.jb51.net *.jbzj.net localhost 1.1.1.1;
if ($invalid_referer) {
rewrite ^/ //www.jb51.net/jb51.gif;
return 412;
break;
}
access_log off;
root /opt/lampp/htdocs/web;
expires 3d;
break;
}

限制仅允许固定IP访问网站,并附加密码验证:

root /opt/htdocs/www;
allow 208.97.167.194;
allow 222.33.1.2;
allow 231.152.49.4;
deny all;
auth_basic “C1G_ADMIN”;
auth_basic_user_file htpasswd;

将多级目录下的文件映射为一个文件,以增强SEO效果:

/job-123-456-789.html 指向/job/123/456/789.html
rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)/.html$ /job/$1/$2/jobshow_$3.html last;

将根目录下的某个文件夹重定向至2级目录:

例如将/shanghaijob/ 指向 /area/shanghai/ 如果将last改为permanent,浏览器地址栏将显示为/location/shanghai/

rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

上述例子存在一个问题:访问/shanghai 时将无法匹配

rewrite ^/([0-9a-z]+)job$ /area/$1/ last; rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

这样/shanghai 也可以正常访问了,但页面中的相对链接将无法使用, 如./list_1.html真实地址是/area/shanghia/list_1.html会变成/list_1.html,导致无法访问。 此时加上自动跳转也是不行的

(-d $request_filename)它有个条件是必需为真实目录,而我的rewrite不是的,所以没有效果
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}

查明原因后就好办了,让我们手动处理跳转逻辑

rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

当文件或目录不存在时进行重定向:

if (!-e $request_filename) {
proxy_pass http://127.0.0.1;
}

域名跳转的配置:

server
{
listen 80;
server_name jump.c1gstudio.com;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/ //www.jb51.net/;
access_log off;
}

多域名转向的配置:

server_name www.c1gstudio.com www.c1gstudio.net;
index index.html index.htm index.php;
root /opt/lampp/htdocs;
if ($host ~ “c1gstudio/.net”) {
rewrite ^(.*) //www.jb51.net$1 permanent;
}

三级域名跳转的配置:

if ($http_host ~* “^(.*)/.i/.c1gstudio/.com$”) {
rewrite ^(.*) //www.jb51.net$1;
break;
}

域名镜像的配置:

server
{
listen 80;
server_name mirror.c1gstudio.com;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/(.*) //www.jb51.net/$1 last;
access_log off;
}

对某个子目录作镜像的配置:

location ^~ /php {
rewrite ^.+ https://www.jb51.net/last;
break;
}

discuz ucenter home (uchome) 的rewrite规则:

rewrite ^/(space|network)-(.+)/.html$ /$1.php?rewrite=$2 last;
rewrite ^/(space|network)/.html$ /$1.php last;
rewrite ^/([0-9]+)$ /space.php?uid=$1 last;
discuz 7 rewrite
rewrite ^(.*)/archiver/((fid|tid)-[/w/-]+/.html)$ $1/archiver/index.php?$2 last;
rewrite ^(.*)/forum-([0-9]+)-([0-9]+)/.html$ $1/forumdisplay.php?fid=$2&page=$3 last;
rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)/.html$ $1/viewthread.php?tid=$2&extra=page/%3D$4&page=$3 last;
rewrite ^(.*)/profile-(username|uid)-(.+)/.html$ $1/viewpro.php?$2=$3 last;
rewrite ^(.*)/space-(username|uid)-(.+)/.html$ $1/space.php?$2=$3 last;
rewrite ^(.*)/tag-(.+)/.html$ $1/tag.php?name=$2 last;
给discuz某版块单独配置域名
server_name bbs.jb51.net news.jb51.net;
location = / {
if ($http_host ~ news/.jb51.net$) {
rewrite ^.+ http://news.jb51.net/forum-831-1.html last;
break;
}
}
discuz ucenter 头像 rewrite 优化
location ^~ /ucenter {
location ~ .*/.php?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location /ucenter/data/avatar {
log_not_found off;
access_log off;
location ~ /(.*)_big/.jpg$ {
error_page 404 /ucenter/images/noavatar_big.gif;
}
location ~ /(.*)_middle/.jpg$ {
error_page 404 /ucenter/images/noavatar_middle.gif;
}
location ~ /(.*)_small/.jpg$ {
error_page 404 /ucenter/images/noavatar_small.gif;
}
expires 300;
break;
}
}
jspace rewrite
location ~ .*/.php?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~* ^/index.php/
{
rewrite ^/index.php/(.*) /index.php?$1 break;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}

附现在常用CMS替换规则

WordPress伪静态规则:

location / {
	index index.html index.php; 
	if (-f $request_filename/index.html){ 
		rewrite (.*) $1/index.html break; 
	} 
	if (-f $request_filename/index.php){ 
		rewrite (.*) $1/index.php; 
	} 
	if (!-f $request_filename){ 
		rewrite (.*) /index.php; 
	} 
} 
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

PHPCMS伪静态规则:

location / {
###以下为PHPCMS 伪静态化rewrite法则
rewrite ^(.*)show-([0-9]+)-([0-9]+)\.html$ $1/show.php?itemid=$2&page=$3;
rewrite ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1/list.php?catid=$2&page=$3;
rewrite ^(.*)show-([0-9]+)\.html$ $1/show.php?specialid=$2;
####以下为PHPWind 伪静态化rewrite法则
rewrite ^(.*)-htm-(.*)$ $1.php?$2 last;
rewrite ^(.*)/simple/([a-z0-9\_]+\.html)$ $1/simple/index.php?$2 last;
}

ECSHOP伪静态规则:

if (!-e $request_filename)
{
rewrite "^/index\.html" /index.php last;
rewrite "^/category$" /index.php last;
rewrite "^/feed-c([0-9]+)\.xml$” /feed.php?cat=$1 last;
rewrite “^/feed-b([0-9]+)\.xml$” /feed.php?brand=$1 last;
rewrite “^/feed\.xml$” /feed.php last;
rewrite “^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$” /category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5&page=$6&sort=$7&order=$8 last;
rewrite “^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)(.*)\.html$” /category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5 last;
rewrite “^/category-([0-9]+)-b([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$” /category.php?id=$1&brand=$2&page=$3&sort=$4&order=$5 last;
rewrite “^/category-([0-9]+)-b([0-9]+)-([0-9]+)(.*)\.html$” /category.php?id=$1&brand=$2&page=$3 last;
rewrite “^/category-([0-9]+)-b([0-9]+)(.*)\.html$” /category.php?id=$1&brand=$2 last;
rewrite “^/category-([0-9]+)(.*)\.html$” /category.php?id=$1 last;
rewrite “^/goods-([0-9]+)(.*)\.html” /goods.php?id=$1 last;
rewrite “^/article_cat-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$” /article_cat.php?id=$1&page=$2&sort=$3&order=$4 last;
rewrite “^/article_cat-([0-9]+)-([0-9]+)(.*)\.html$” /article_cat.php?id=$1&page=$2 last;
rewrite “^/article_cat-([0-9]+)(.*)\.html$” /article_cat.php?id=$1 last;
rewrite “^/article-([0-9]+)(.*)\.html$” /article.php?id=$1 last;
rewrite “^/brand-([0-9]+)-c([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)\.html” /brand.php?id=$1&cat=$2&page=$3&sort=$4&order=$5 last;
rewrite “^/brand-([0-9]+)-c([0-9]+)-([0-9]+)(.*)\.html” /brand.php?id=$1&cat=$2&page=$3 last;
rewrite “^/brand-([0-9]+)-c([0-9]+)(.*)\.html” /brand.php?id=$1&cat=$2 last;
rewrite “^/brand-([0-9]+)(.*)\.html” /brand.php?id=$1 last;
rewrite “^/tag-(.*)\.html” /search.php?keywords=$1 last;
rewrite “^/snatch-([0-9]+)\.html$” /snatch.php?id=$1 last;
rewrite “^/group_buy-([0-9]+)\.html$” /group_buy.php?act=view&id=$1 last;
rewrite “^/auction-([0-9]+)\.html$” /auction.php?act=view&id=$1 last;
rewrite “^/exchange-id([0-9]+)(.*)\.html$” /exchange.php?id=$1&act=view last;
rewrite “^/exchange-([0-9]+)-min([0-9]+)-max([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$” /exchange.php?cat_id=$1&integral_min=$2&integral_max=$3&page=$4&sort=$5&order=$6 last;
rewrite ^/exchange-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$” /exchange.php?cat_id=$1&page=$2&sort=$3&order=$4 last;
rewrite “^/exchange-([0-9]+)-([0-9]+)(.*)\.html$” /exchange.php?cat_id=$1&page=$2 last;
rewrite “^/exchange-([0-9]+)(.*)\.html$” /exchange.php?cat_id=$1 last;
}

SHOPEX伪静态规则 :

location / {
if (!-e $request_filename) {
rewrite ^/(.+\.(html|xml|json|htm|php|jsp|asp|shtml))$ /index.php?$1 last;
}
}

SaBlog 2.0:

# 只带月份的归档
rewrite "^/date/([0-9]{6})/?([0-9]+)?/?$" /index.php?action=article&setdate=$1&page=$2 last;
# 无分类翻页
rewrite ^/page/([0-9]+)?/?$ /index.php?action=article&page=$1 last;
# 分类
rewrite ^/category/([0-9]+)/?([0-9]+)?/?$ /index.php?action=article&cid=$1&page=$2 last;
rewrite ^/category/([^/]+)/?([0-9]+)?/?$ /index.php?action=article&curl=$1&page=$2 last;
# 归档、高级搜刮
rewrite ^/(archives|search|article|links)/?$ /index.php?action=$1 last;
# 全数批评、标签列表、引用列表 带分页
rewrite ^/(comments|tagslist|trackbacks|article)/?([0-9]+)?/?$ /index.php?action=$1&page=$2 last;
# tags
rewrite ^/tag/([^/]+)/?([0-9]+)?/?$ /index.php?action=article&item=$1&page=$2 last;
# 文章
rewrite ^/archives/([0-9]+)/?([0-9]+)?/?$ /index.php?action=show&id=$1&page=$2 last;
# RSS rewrite ^/rss/([0-9]+)?/?$ /rss.php?cid=$1 last;
rewrite ^/rss/([^/]+)/?$ /rss.php?url=$1 last;
# 用户 rewrite ^/uid/([0-9]+)/?([0-9]+)?/?$ /index.php?action=article&uid=$1&page=$2 last;
rewrite ^/user/([^/]+)/?([0-9]+)?/?$ /index.php?action=article&user=$1&page=$2 last;
# 舆图文件
rewrite sitemap.xml sitemap.php last;
# 自界说链接
rewrite ^(.*)/([0-9a-zA-Z\-\_]+)/?([0-9]+)?/?$ $1/index.php?action=show&alias=$2&page=$3 last;

Discuz 7伪静态规则 :

rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1 last;
rewrite ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2 last;
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page\%3D$3&page=$2 last;
rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?$1=$2 last;
rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;

Typecho:

location / {
index index.html index.php;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}

实例:shopex启用伪静态

……
    location /
    {
             if (!-e $request_filename) {
             rewrite ^/(.*)$ /index.php?$1 last;
    }
    }
……

到此这篇关于Nginx伪静态配置和常用Rewrite伪静态规则集锦的文章就介绍到这了,更多相关Nginx伪静态配置和常用Rewrite伪静态规则内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

上一篇
Nginx超时配置详解:从客户端连接到上游代理

Nginx通过多种超时机制管理连接生命周期以提升性能。核心参数包括控制TCP长连接的keepalive_timeout、限制请求头与体接收速度的client_header/body_timeout,以及处理响应发送的send_timeout。此外,针对反向代理场景,还需配置与上游服务器的连接、读取及发送超时时间,合理设置这些值可防止资源耗尽并优化并发处理能力。

下一篇
Apache开启.htaccess及使用方法详解

本文介绍如何在本地Apache环境中启用.htaccess文件,主要涉及修改httpd.conf中的AllowOverride参数。同时详细解析.htaccess的多种实用功能,包括自定义错误页面、目录密码保护、IP访问限制、URL自动转向、默认首页设置以及防止图片盗链等配置方法,并分析了使用.htaccess对服务器性能和安全性的潜在影响。

相关文章

更多
Nginx配置中rewrite指令last与break的区别分析

在Nginx的rewrite配置中,last与break指令在处理请求重定向时存在本质差异。last会停止当前请求并重新发起匹配,可能引发死循环或改变访问路径;break则跳过当前rewrite阶段,直接执行后续处理。在使用root、alias或proxy_pass指定源时,不同location结构下两者的行为表现各异,需根据具体场景选择合适指令以避免访问错误。

2025.07.31 937
Nginx Rewrite配置详解与基于URL参数的Location路由

本文介绍在旧项目迁移场景中,如何利用Nginx的rewrite指令实现URL重定向,以及如何通过if语句结合$query_string变量,根据GET请求参数将流量分发至不同的upstream。内容涵盖rewrite语法、执行顺序、大小写匹配技巧,以及基于参数的条件路由配置方法。

2025.08.18 807
Nginx 转发匹配规则与 Rewrite 指令详解

本文详细解析 Nginx 的 URL 重写与转发机制,涵盖区分大小写的正则匹配符、文件目录存在性判断以及 last、break、redirect 等 Flag 标记的行为差异。通过对比 Apache 规则,提供 break、if、return、set 等核心指令的语法说明,并列举了防盗链、缓存设置、域名跳转及 SEO 友好的伪静态实现等实际配置案例,帮助开发者掌握 Nginx 高级路由控制技巧。

2026.05.16 790
Nginx Rewrite实现URL跳转与CDN加速配置详解

本文介绍利用Nginx rewrite模块实现URL跳转及静态资源CDN加速的方法。通过正则表达式匹配请求路径,配合last、break、redirect和permanent等标记,可完成域名迁移或设备适配。同时展示如何结合location指令将图片等静态文件请求重定向至CDN域名,以提升加载速度并节省源站带宽。

2025.12.12 630
Nginx Rewrite 应用场景、语法详解与实战配置

本文详解 Nginx Rewrite 模块在 URL 跳转、伪静态处理及 SEO 优化中的核心应用。通过解析 rewrite 指令的四种 Flag(last、break、redirect、permanent)差异,结合外部跳转、内部跳转及 HTTP 转 HTTPS 等具体案例,展示如何精准控制请求流程与状态码返回,提升网站安全性与规范性。

2026.05.31 519
Nginx重定向配置详解:Rewrite规则与IF条件判断

本文介绍Nginx中实现URL重定向的核心机制,重点解析rewrite命令的语法结构及last、break、redirect、permanent等flag标记的区别。同时详细说明在server和location块中使用IF条件进行正则匹配、文件目录判断以及return状态码返回的配置方法,并提供合并域名或防盗链等实际场景的代码示例。

2026.03.05 519

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。