【发布时间】:2015-04-25 14:02:41
【问题描述】:
好的,首先让我说我明白这是一个以前被问过的问题。我只是无法将其缩小到关键字并找到我正在寻找的内容。如果这是重复的,请提前抱歉。 htaccess 重写对我来说就像黑魔法......我真的无法让它工作。
对于手头的问题: 我正在编写一个简单的准系统 php/html 站点。我已经有 10 年没有这样做了(我通常使用一些 CMS(wordpress、joomla 等))。我正在尝试处理这些 CMS 中“免费提供”的一些东西。喜欢漂亮的网址。 我有一个简单的 index.php,其中包含一些用于构建页面的内容。然后我有了包含动态内容的包含文件夹。
那么两个实际 URL 的例子
index.php?page=index(我的主页) index.php?page=anotherpage(另一页)
但是如果我想去 index.php?page=a-sub-page-to-another-page
这是我的 PHP(web 根文件夹中的 index.php)
if(isset($_GET["page"])){
$page = $_GET["page"];
$filename = "/includes/" . $page . ".php";
if(file_exists($filename)){
include("/includes/head.php");
include("/includes/navbar.php");
include $filename;
include("/includes/footer.php");
}else{
include("/includes/404.php");
}
}else{
include("/includes/head.php");
include("/includes/navbar.php");
include("/includes/index.php");
include("/includes/footer.php");
}
这是我的 .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1
只要我没有任何子页面,它就可以工作。但是如果我尝试去 [root]/somepage/somsubpage 那么它就不再起作用了。有人可以帮我吗?我希望复制我使用 wordpress 等标准 CMS 获得的效果,其中所有 URL 都是 SEO 友好的。
【问题讨论】:
标签: php .htaccess mod-rewrite