如何设置 PHP 伪静态:使用 mod_rewrite:启用 mod_rewrite 并创建 .htaccess 文件,将以 /index.php/ 开头的 URL 重定向到等效的 URL。使用 $_GET 超全局变量:获取请求路径,解析路径以提取路径部分,移除脚本名称并进行必要的处理以生成重写后的 URL。
如何在 PHP 中设置伪静态
伪静态允许你使用更具描述性且用户友好的 URL 而无需更改服务器配置。在 PHP 中实现伪静态有两种主要方法。
第一种方法:使用 mod_rewrite
LoadModule rewrite_module modules/mod_rewrite.so
RewriteRule ^index\.php/(.*)$ /$1 [L]
这将重写以 /index.php/ 开头的 URL,并将其重定向到其等效的 URL(不带 /index.php/)。
立即学习“PHP免费学习笔记(深入)”;
第二种方法:使用 $_GET 超全局变量
$path = str_replace('/index.php', '', $path);
示例:
<?php // 获取请求路径 $path = $_SERVER['REQUEST_URI']; // 解析路径 $pathInfo = parse_url($path); // 移除脚本名称 $path = str_replace('/index.php', '', $pathInfo['path']); // 处理重写 if ($path == '/about-us') { header('Location: /page/about'); } ?>
这将重写 /index.php/about-us URL 到 /page/about。
以上就是php如何设置伪静态的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号