PHP 伪静态(pseudo-static)是指通过服务器配置和 PHP 脚本配合,将动态网页的 URL 显示为静态网页的 URL 格式。这种技术可以提高网站的 SEO 效果,使 URL 更加友好,便于用户记忆和分享。
.htaccess 文件:通过重写规则实现伪静态。example.com/article/123。example.com/product/456。example.com/about-us。.htaccess 文件假设你有一个 PHP 文件 article.php?id=123,你想将其转换为 article/123 的形式。
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([0-9]+)/?$ article.php?id=$1 [L]同样的需求,Nginx 的配置如下:
server {
listen 80;
server_name example.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}然后在 index.php 中处理伪静态:
<?php
if (isset($_SERVER['PATH_INFO'])) {
$path = explode('/', trim($_SERVER['PATH_INFO'], '/'));
if (count($path) > 1 && $path[0] == 'article') {
$id = $path[1];
// 处理文章逻辑
}
}
?>原因:可能是服务器配置文件中的重写规则不正确,或者 PHP 脚本没有正确处理伪静态 URL。
解决方法:
.htaccess 或 Nginx 配置文件:确保重写规则正确无误。原因:可能是服务器没有正确配置伪静态规则,或者 PHP 脚本没有正确处理请求。
解决方法:
.htaccess 或 Nginx 配置文件中的重写规则正确。通过以上方法,你可以有效地解决 PHP 伪静态相关的问题,提升网站的 SEO 效果和用户体验。