简体   繁体   English

htaccess用php页面重写

[英]htaccess rewrite with php pages

OK, first of let me just say I understand that this is a question that has been asked before. 好,首先,我只想说我理解这是之前已经提出的问题。 I just can't narrow it down to keywords and find what I'm looking for. 我只是不能将其范围缩小到关键字并找到我想要的。 So sorry in advance if this is a duplicate. 因此,如果这是重复的操作,请提前抱歉。 htaccess rewrite is like black magic to me... I can't really get it to work. htaccess重写对我来说就像是黑魔法……我真的无法使它正常工作。

To the question at hand: I'm writing a simple barebone php/html site. 对于当前的问题:我正在编写一个简单的准系统php / html网站。 I haven't done this in about 10 years (I usually use some CMS (wordpress, joomla etc.)). 我大约10年没有这样做(我通常使用一些CMS(wordpress,joomla等))。 I'm trying to get a handle on some of the things that "come for free" with these CMSs. 我正在尝试处理这些CMS的“免费提供”的某些功能。 Like pretty URLs. 就像漂亮的网址。 I have a simple index.php with some includes to build the pages. 我有一个简单的index.php,其中包含一些用于构建页面的内容。 Then I have my includes folder with my dynamic content. 然后,我有了包含我的动态内容的包含文件夹。

So two case examples of the actual URLs 那么实际网址的两个案例示例

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

But what if I want to go to index.php?page=a-sub-page-to-another-page 但是,如果我想转到index.php,该怎么办?page = a-sub-page-to-other-page

This is my PHP (index.php in web root folder) 这是我的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");
    }

This is my .htaccess 这是我的.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1

This works as long as I don't have any sub pages. 只要我没有任何子页面,此方法就起作用。 But If I try to go to [root]/somepage/somsubpage then it doesn't work anymore. 但是,如果我尝试转到[root] / somepage / somsubpage,那么它将不再起作用。 Can someone please help me out here? 有人可以帮我吗? I'm looking to replicate the effect I get with standard CMSs like wordpress, where all URLs are SEO friendly. 我希望复制使用标准CMS(如wordpress)获得的效果,其中所有URL都是SEO友好的。

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

no in you php variable $_GET['page'] will have full url for example: php变量$ _GET ['page']中没有完整的URL,例如:

example.com/foo/barr example.com/foo/barr

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

However this is the first part only you would need do special function to map url to your page. 但是,这是第一部分,只有您需要执行特殊功能才能将url映射到您的页面。

do SEO pages is to store do something like: example.com/some-url/of-my-special/page-in-here.1111.html so this is your url you make it as so you need to look at .xxxxx.html xxxx is variable so now if u write htaccess like: 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 $ _GET ['fullurl'] => /some-url/of-my-special/page-in-here.1111.html $ _GET ['pageid'] => 1111

Please! 请! If anyone reads this... I'm still stuck on what to do... And I can't find anything but mysql related articles. 如果有人读过这本书...我仍然在做些什么...我只能找到与mysql相关的文章。 I've searched for days. 我搜索了几天。 I just need to understand how to rewrite the PHP in my original post to work with sub-pages, dynamically with variables. 我只需要了解如何在我的原始文章中重写PHP,以动态地使用变量来处理子页面。 Ex. 防爆。 index.php?page=index, index.php?page=secondPage, index.php?page=secondPage&SubPage <-- this is the part I can't find anything on. index.php?page = index,index.php?page = secondPage,index.php?page = secondPage&SubPage <-这是我找不到任何内容的部分。 How do I get the php to look for more than one level strings? 我如何获得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