不同类型的URL

I see websites where they have an index page and then their url reads

index.php?page=page

I was wondering how i would do that for my own website using a folder called pages and loading the content of those pages onto my index page while using that url type.

If that makes sense.

Say you have following structure:

index.php
pages
    page1.php
    page2.php
    pageN.php

So make use of $_GET to get the page name:

index.php

if(isset($_GET['page']) && !empty($_GET['page'])) {
    $pagename = $_GET['page'];
    if(file_exists("pages/$pagename.php")){
        include_once "pages/$pagename.php";
    }
}

When URL has index.php?page=page1, it checks for page1.php inside pages folder. If file exists, it includes that file inside index.php. Similarly for other pages.


This is a simple outline idea though. You can develop further, introduce validations. And again, use pretty URLs to make it better for SEO. It can be done by adding few lines to .htaccess file. URL then will be domain.com/page1 or something as you configure.