Pretty Urls With Master Page?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
johnywhy
Forum Newbie
Posts: 5
Joined: Sun Dec 09, 2007 2:18 pm

Pretty Urls With Master Page?

Post by johnywhy »

hi, my site has header, footer, and sidebar which repeat on all pages. my goal is to contain the layout, header, footer, sidebar, and content all in separate files, and assemble them with php. problem-- getting pretty urls.

my current structure has only 1 url. each subpage is loaded with a parameter in the url indicating what to load into the content area of the page. for example:

About page url: http://mysite.org/master?contentpage=about
Events page url: http://mysite.org/master?contentpage=events

problem: i want these pages to have prettier urls for sharing. I mean, i want them to have simple urls which make them appear to be static html pages:
http://mysite.org/about
http://mysite.org/events

so question, is there way to achieve that with my current structure? If not, is there a different structure which will achieve simple urls, yet still keep layout in one file, and header, footer, sidebar, and content in other files?

my master page:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Include Test</title>        
    </head>    
    <body>        
        <TABLE WIDTH="100%" border=1>
            <TR>
                <TD class="headercell" colspan="3">
                    HEADER<br>
                    <? include "header.php"; ?>
                </TD>
            </TR>
            <TR>
                <TD class="leftsidebar">LEFT SIDEBAR
                    <? include "leftsidebar.php"; ?></TD>
                <TD class="bodycell">BODY CONTENT
                    <br>
                    <? $pagename=$_GET["contentpage"]; ?>
                    <? include $pagename.".php"; ?></TD>
                <TD>RIGHT SIDE-BAR<br>
                    <? include "rightsidebar.php"; ?>
                </TD>
            </TR>
            <TR>
                <TD colspan="3">FOOTER</TD>
            </TR>
        </TABLE>        
    </body>    
</html>
​
yes, yes, i know tables are the devil, you don't have to tell me. please keep replies to the question above.
User avatar
axcrow
Forum Newbie
Posts: 9
Joined: Mon Aug 09, 2010 12:22 pm

Re: Pretty Urls With Master Page?

Post by axcrow »

You could redirect everything to index.php with htaccess - mod_rewrite - without changing the url - hidden redirect.
Then in index.php you parse the $_SERVER['REQUEST_URI'].
for example you could explode the request_uri by '?' and then by '/' - or do it with regexp
finally you include the 'page' you got in your master page =) just dont forget to validate first - since it is user input.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Pretty Urls With Master Page?

Post by josh »

You should look into concepts of MVC & Routing if you want beatiful URLs and organized codebases. Here's some random tutorial http://allseeing-i.com/Bare-bones-Rails ... er-for-PHP
Post Reply