伪静态(pseudo-static)是指通过服务器配置和编程技巧,将动态网页的URL显示为静态网页的URL形式。虽然URL看起来像是静态的(例如:http://example.com/article/123.html
),但实际上仍然是动态生成的页面。
.htaccess
文件进行URL重写。mod_rewrite
模块。.htaccess
文件,添加以下内容:RewriteEngine On
RewriteBase /
RewriteRule ^article/([0-9]+)\.html$ article.php?id=$1 [L]
article.php
文件中处理请求:<?php
$id = $_GET['id'];
// 根据$id获取文章内容并显示
?>
nginx.conf
或default.conf
),添加以下内容:server {
listen 80;
server_name example.com;
location /article {
rewrite ^/article/([0-9]+)\.html$ /article.php?id=$1 last;
}
location / {
root /path/to/your/website;
index index.php index.html index.htm;
}
}
article.php
文件中处理请求:<?php
$id = $_GET['id'];
// 根据$id获取文章内容并显示
?>
router.php
),添加以下内容:<?php
$uri = $_SERVER['REQUEST_URI'];
preg_match('/^\/article\/([0-9]+)\.html$/', $uri, $matches);
if (isset($matches[1])) {
$id = $matches[1];
include 'article.php';
} else {
// 处理其他请求
}
?>
article.php
文件中处理请求:<?php
$id = $_GET['id'];
// 根据$id获取文章内容并显示
?>
.htaccess
或Nginx配置文件中的重写规则是否正确。通过以上方法,你可以实现PHP伪静态程序,提升用户体验和SEO优化。
领取专属 10元无门槛券
手把手带您无忧上云