I am having a problem learning how proper website structure should be. And by that I mean how to code the pages and how folder structure should be.

Currently I am navigating around my website using GET variables in PHP when you want go to another page, so I am always loading the index.php file. And I would load the page I wanted like so:

$page = "error";

if(isset($_GET["page"]) AND file_exists("pages/".$_GET["page"].".php")) {

    $page = $_GET["page"];

} elseif(!isset($_GET["page"])) {

    $page = "home";


And this:

<div id="page">

The reason I am doing this is because it makes making new pages a lot easier as I don't have to link style sheets and javascript in every file like so:

        Website Name

    <link href='http://fonts.googleapis.com/css?family=Lato:300,400' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="css/style.css" type="text/css">
    <link rel="shortcut icon" href="favicon.png"/>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js" type="text/javascript"></script>


There is a lot of problems doing it this way as URLs don't look normal ("?page=apanel/app") if I am trying to access a page from inside a folder inside the pages folder to prevent clutter.

Obviously this is not the only way to go about it and I can't find the proper way to do it because I'm sure websites don't link style sheets in every file as that would be very bad if you changed a file name or needed to add another file you would have to change it in every file.

If anyone could tell me how it is actually done or point me towards a tutorial that would be great.

Thanks for your time :)

1 Answer 1


You may not realise it, but you're actually using the Front Controller design pattern. It's very common to use a single index.php as the starting point for every page. All of the big PHP MVC frameworks (such as Zend, Symfony, Laravel) do this.

In order to make the URLs look nice, you usually need to add something into your Apache configuration (assuming that Apache is your web server). With Apache, this is typically done by adding a RewriteRule to a file .htaccess that translates a path such as /mypage into a path such as /index.php?page=mypage.

The Laravel installation guide here http://laravel.com/docs/installation#pretty-urls gives a typical example of what should be in the .htaccess.

A word of warning, the code you pasted in your question has some dangerous security flaws. An attacker could easily make your page include files you didn't intend by passing in URLs such as /index.php?page=../../../../etc/passwd or something like that. You should sanitise the input (perhaps by removing all '/' characters) before using it in the 'include' line.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.