伪静态让栏目、文章等漂亮网址能正确转到 PHP。配错会出现:安装页异常、后台 404、前台栏目打不开。
原则:网站运行目录 =
zonecms/public;伪静态规则作用在该目录(或 server 的 root 已指向 public)。一、怎么判断伪静态是否生效?
- 能打开
/install、/、/admin/login - 打开某个栏目 URL(非
index.php?s=这种)不报 404
若只有带 index.php 的地址能开,说明伪静态未生效。
二、Nginx(含宝塔)
程序根目录提供了样例 nginx.conf.sample,核心如下(请把路径和域名改成你的):
server {
listen 80;
server_name www.example.com;
root /www/wwwroot/yoursite/public; # 必须是 public
index index.html index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000; # 按面板实际改,如 unix socket
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* runtime|\.env {
deny all;
}
}
宝塔:网站 → 设置 → 伪静态,可选用「thinkphp」规则,或确保等价于上面的 try_files … /index.php?$query_string。
三、小皮面板 / phpstudy(Nginx)
项目已提供:public/nginx.htaccess,内容为:
# ZoneCMS / ThinkPHP 伪静态(小皮 Nginx 会 include 到 location / 内)
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
- 确认站点根目录指向
…/zonecms/public - 确认该文件存在于
public/nginx.htaccess - 在面板中重启 Nginx
若小皮未自动加载此文件,请把上述规则粘贴到站点 Nginx 配置的 location / 中。
四、Apache
使用 public/.htaccess(已随包提供):
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
还需要:
- 启用模块
mod_rewrite - 虚拟主机中
AllowOverride All(允许读取 .htaccess) - DocumentRoot 指向
…/public
五、IIS(简要)
需安装 URL Rewrite,将请求重写到 index.php。虚拟目录指向 public。具体规则可按 ThinkPHP 官方 IIS 伪静态配置。
六、常见错误对照
| 现象 | 常见原因 | 处理 |
|---|---|---|
| 全站 404 | 根目录不是 public | 改网站根目录到 public |
| 只有首页能开 | 伪静态未生效 | 按上文补规则并重启 Web 服务 |
| css/图片 404 | 根目录指错或静态路径不对 | 检查 public/static 能否直接访问 |
| 装完又进安装向导 | 缺少 install.lock | 在 public 下新建空文件 install.lock |
七、和「静态 HTML 生成」的区别
伪静态:让动态 PHP 路由支持漂亮 URL,安装后就应配置。
静态生成:后台把页面生成真正的 .html 文件,属于可选加速,装完不必马上做。