skip to Main Content

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.

2

Answers


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

    example.com/foo/barr

    so $_GET[‘page’] => /foo/barr

    However this is the first part only you would need do special function to map url to your page.

    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:

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

    $_GET[‘fullurl’] => /some-url/of-my-special/page-in-here.1111.html
    $_GET[‘pageid’] => 1111

    Login or Signup to reply.
  2. Please! If anyone reads this… I’m still stuck on what to do… And I
    can’t find anything but mysql related articles. 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. Ex.
    index.php?page=index, index.php?page=secondPage,
    index.php?page=secondPage&SubPage <– this is the part I can’t find
    anything on. How do I get the php to look for more than one level
    strings?

    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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search