织梦nginx伪静态规则怎么写
织梦默认生成静态 HTML,URL 形如 /a/2026/0729/123.html,路径深、不美观。通过 Nginx 伪静态可以把 URL 改成 /news/123.html 这种短地址,同时不增加静态文件数量。本文给出完整可用的规则。
一、伪静态的前置准备
- 后台
系统 - 系统基本参数 - 核心设置,开启“是否使用伪静态”; 是否使用动态页面:选“否”,让列表页和文章页都走动态接口plus/list.php、plus/view.php;- 确认 Nginx 已加载
ngx_http_rewrite_module(默认编译就有)。
二、Nginx 完整伪静态规则
server {
listen 80;
server_name www.yourdomain.com;
root /www/wwwroot/yourdomain.com;
index index.html index.php;
# 首页
location / {
if (!-e $request_filename) {
rewrite ^/$ /plus/list.php?tid=1 last;
}
}
# 列表页:/news/ 或 /news/list_1_2.html
location ~ ^(/[^/]+)/?$ {
if (!-e $request_filename) {
rewrite ^/([^/]+)/?$ /plus/list.php?dirname=$1 last;
}
}
location ~ ^/([^/]+)/list_([0-9]+)_([0-9]+)\.html$ {
rewrite ^/([^/]+)/list_([0-9]+)_([0-9]+)\.html$ /plus/list.php?tid=$2&PageNo=$3 last;
}
# 文章页:/news/123.html
location ~ ^/([^/]+)/([0-9]+)\.html$ {
rewrite ^/([^/]+)/([0-9]+)\.html$ /plus/view.php?aid=$2 last;
}
# TAG 页:/tags/abc.html 或 /tags/abc/2.html
location ~ ^/tags/([^/]+)/([0-9]+)\.html$ {
rewrite ^/tags/([^/]+)/([0-9]+)\.html$ /plus/tag.php?/$1/$2 last;
}
location ~ ^/tags/([^/]+)\.html$ {
rewrite ^/tags/([^/]+)\.html$ /plus/tag.php?/$1 last;
}
# 搜索页:/search/关键词.html
location ~ ^/search/(.+)\.html$ {
rewrite ^/search/(.+)\.html$ /plus/search.php?q=$1 last;
}
# PHP 处理
location ~ \.php$ {
try_files $uri=404;
fastcgi_pass unix:/tmp/php-cgi-74.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
}
三、各规则详解
1. 列表页伪静态
原始 URL:/plus/list.php?tid=5&PageNo=2
伪静态后:/news/list_5_2.html
规则用 ([0-9]+) 捕获 tid 和 PageNo,last 表示把重写后的请求交回 location 重新匹配。
2. 文章页伪静态
原始 URL:/plus/view.php?aid=123
伪静态后:/news/123.html
如果想带日期,可改为 /news/2026/0729/123.html,规则:
rewrite ^/([^/]+)/([0-9]{4})/([0-9]{4})/([0-9]+)\.html$ /plus/view.php?aid=$4 last;
3. TAG 页伪静态
原始 URL:/plus/tag.php?/关键词/2
伪静态后:/tags/关键词.html
注意 tag 含中文需 Nginx 支持 UTF-8,且 URL 编码后可能很长。
四、中文 tag 与特殊字符处理
中文 tag 在 Nginx 默认会被百分号编码,规则要兼容:
if ($request_uri ~* "^/tags/(.+)\.html$") {
set $tag_name $1;
}
location /tags/ {
rewrite ^/tags/(.*)$ /plus/tag.php?/$1 last;
}
同时后台 系统 - 系统基本参数 - 核心设置 里把“TAG 标签伪静态”开启。
五、规则优先级
Nginx location 匹配优先级:精确 = > 前缀 ^~ > 正则 ~ > 普通前缀。多条正则按书写顺序匹配,命中即停止。因此上面规则顺序为:列表 → 文章 → TAG → 搜索,避免互相覆盖。
六、验证与排错
- 修改规则后
nginx -t检查语法,再nginx -s reload; - 访问伪静态 URL,查看 Nginx 日志
/www/wwwlogs/yourdomain.com.log里的 rewrite 结果; - 如果返回 404,多半是规则没命中;如果返回 PHP 源码,是 PHP location 没生效。
可在规则里加
rewrite_log on; 并把 error_log 调到 notice 级别,查看 rewrite 详细过程,调试完关掉。七、注意事项
- 伪静态后务必把
robots.txt与 sitemap 里的 URL 一并更新; - 原静态 HTML 文件(
/a/下)建议保留一段时间并 301 到新 URL; - 百度收录切换需要 1~2 周过渡期,期间不要删除旧 URL;
- 伪静态会加重 PHP 与 MySQL 负载,建议配合 OPcache 与 Redis 缓存。
把上述规则跑通后,织梦 URL 既美观又利于 SEO。建议先用一个栏目测试规则无误,再全站切换,并持续关注百度搜索资源平台的索引量与抓取诊断,让伪静态改造平稳过渡、收录不降反升。