samedi 25 avril 2015

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.

To the question at hand: I'm writing a simple barebone php/html site. I haven't done this in about 10 years (I usually use some CMS (wordpress, joomla etc.)). I'm trying to get a handle on some of the things that "come for free" with these CMSs. Like pretty URLs. I have a simple index.php with some includes to build the pages. 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)

But what if I want to go to index.php?page=a-sub-page-to-another-page

This is my PHP (index.php in web root folder)

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

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. 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.


Aucun commentaire:

Enregistrer un commentaire