【发布时间】:2017-12-06 15:58:26
【问题描述】:
所以我正在尝试了解 .htaccess RewriteRules 并面临以下问题:
“/home”应该打开“index.php?page=home”
我在“index.php”中有一个 php $_GET['page'],但它每次都通过“index.php”而不是“home”。
这是文件
.htaccess
RewriteEngine On
RewriteRule ^/?(.+)/?$ index.php?page=$1 [L]
index.php PHP 部分
<?php
if(isset($_GET['page'])) {
$path = $_GET['page'] . ".php";
if (file_exists($path)) {
include($path);
}
else {
echo "File not found: " . $path;
}
}
else {
include("home.php");
}
?>
输出总是:
File not found: index.php.php
我在这两个方面都遇到了这个问题:web-server 和 apache。
有什么建议吗?如有必要,我会用更多信息更新我的问题。
【问题讨论】:
-
试试这个
RewriteRule ^(.*)(/|)$ /index.php?page=$1 [QSA,L](我没有在我们的服务器上测试过,但它和我们的非常相似) -
如果你有一组已知的页面,你可以使用类似
RewriteRule ^(home|about|catalog)(/|)$ /$1.php [QSA,L]
标签: php apache .htaccess mod-rewrite url-rewriting