Jump to content

Dynamic urls, how?


Bikkebakke

Recommended Posts

Hi freaks! It's been a while since I last asked for help  ;)

 

I'm done with my first learning project and decided to start a new one with proper planning etc.

I want to use dynamic urls but I was unable to find a good tutorial so I decided to ask here, so;

 

Does anyone know a good tutorial which I could learn from or could someone give me a brief explanation on how to create such a system?

 

The idea is to make a page with header, footer, menu and a (content) frame/div where I include() the page.

 

 

Thanks in advance,

Bikkebakke

 

Link to comment
Share on other sites

Well assuming you're just wanting the one level of dynamic-ness (e.g. domain.com/page1, domain.com/page2 etc. not domain.com/page1/page2) you could use a .htaccess file and some simple PHP with a directory for the included files like so:

 

.htaccess

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

 

index.php

//header goes here
include('/pages/'.str_replace('/', '', $_SERVER['REQUEST_URI']).'.php');
//footer goes here

 

Then you have a director called pages which has a file for each page (page1.php, page2.php etc). Easy. The htaccess sends all requests to the index.php file, which has the header and footer, and in between it includes the file from the pages directory that matches the bit of the url after the domain (minus the slash).

 

Also, if you wanted it to do a 404 message for pages that weren't in the pages directory, you could adapt index.php to look like this:

 

//header goes here
$page = '/pages/'.str_replace('/', '', $_SERVER['REQUEST_URI']).'.php';
if (file_exists($page))
  include($page);
else
  print '<h1>Page not found</h1><p>The page you were looking for could not be found</p>';
//footer goes here

Link to comment
Share on other sites

Thanks for that, dooper3.

 

Theres no .htaccess file on the server (forced to show hidden files too) so I'd assume I have to make one, is there anything else I need to include in the file than just the lines you quoted?

 

Like, are the following lines required?

AuthType
AuthName
AuthUserFile
Require

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.