htaccess用php页面重写



好的,首先让我说我理解这是一个以前问过的问题。我只是无法将其缩小到关键字范围并找到我想要的内容。如果这是重复的,请提前表示歉意。htaccess重写对我来说就像魔术……我真的无法让它发挥作用。

对于手头的问题:我正在写一个简单的barebone php/html网站。我已经有10年没有做过这件事了(我通常使用一些CMS(wordpress、joomla等)。我正在努力掌握这些CMS"免费"的一些东西。喜欢漂亮的URL。我有一个简单的index.php,其中包含一些构建页面的内容。然后我就有了包含动态内容的包含文件夹。

因此,实际URL 的两个案例

index.php?page=索引(我的主页)index.php?page=另一页(另一页)

但是如果我想去index.php?page=a子页到另一页

这是我的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友好的。

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1

php变量$_GET['page']中没有完整的url,例如:

example.com/foo/barr

所以$_GET['page']=>/foo/barr

然而,这是第一部分,只有你需要做特殊的功能来将url映射到你的页面。

做SEO页面是存储做一些类似的事情:example.com/some-url/of-my-special/page-in-here.1111.html所以这是你的url,你需要查看它。xxxxx.html xxxx是可变的,所以现在如果你写htaccess类似:

RewriteRule ^(.*).(d+).html$ index.php?fullurl=$1&pageid=$2

$_GET['fullurl']=>/some-url/我的特殊/页面中的个人.1111.html$_GET['pageid']=>1111

请!如果有人读到这个。。。我仍然纠结于该做什么除了mysql相关的文章,什么都找不到。我已经搜索过了天。我只需要了解如何在我的原始版本中重写PHPpost使用子页面,动态使用变量。Ex。index.php?page=index,index.php?page=secondPage,index.php?page=secondPage&子页<--这是我找不到的部分上的任何内容。我如何让php查找多个级别字符串?

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?level1=$1
RewriteRule ^([^/]+)/([^/])+$ index.php?level1=$1&level2=$2
RewriteRule ^([^/]+)/([^/])+/([^/])+$ index.php?level1=$1&level2=$2&level3=$3

最新更新