繁体   English   中英

htaccess用php页面重写

[英]htaccess rewrite with php pages

好,首先,我只想说我理解这是之前已经提出的问题。 我只是不能将其范围缩小到关键字并找到我想要的。 因此,如果这是重复的操作,请提前抱歉。 htaccess重写对我来说就像是黑魔法……我真的无法使它正常工作。

对于当前的问题:我正在编写一个简单的准系统php / html网站。 我大约10年没有这样做(我通常使用一些CMS(wordpress,joomla等))。 我正在尝试处理这些CMS的“免费提供”的某些功能。 就像漂亮的网址。 我有一个简单的index.php,其中包含一些用于构建页面的内容。 然后,我有了包含我的动态内容的包含文件夹。

那么实际网址的两个案例示例

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

但是,如果我想转到index.php,该怎么办?page = a-sub-page-to-other-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,那么它将不再起作用。 有人可以帮我吗? 我希望复制使用标准CMS(如wordpress)获得的效果,其中所有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/of-my-special/page-in-here.1111.html $ _GET ['pageid'] => 1111

请! 如果有人读过这本书...我仍然在做些什么...我只能找到与mysql相关的文章。 我搜索了几天。 我只需要了解如何在我的原始文章中重写PHP,以动态地使用变量来处理子页面。 防爆。 index.php?page = index,index.php?page = secondPage,index.php?page = secondPage&SubPage <-这是我找不到任何内容的部分。 我如何获得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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM