Bikkebakke Posted March 23, 2010 Share Posted March 23, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/196232-dynamic-urls-how/ Share on other sites More sharing options...
dooper3 Posted March 23, 2010 Share Posted March 23, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/196232-dynamic-urls-how/#findComment-1030519 Share on other sites More sharing options...
Bikkebakke Posted March 23, 2010 Author Share Posted March 23, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/196232-dynamic-urls-how/#findComment-1030531 Share on other sites More sharing options...
Bikkebakke Posted March 23, 2010 Author Share Posted March 23, 2010 Okay I got it working, except, what format should the file follow at the pages-directory? I.e. <a href="?id=1">Home</a> What does the "?id=1" refer to in the ./pages/ and how do I define it? Quote Link to comment https://forums.phpfreaks.com/topic/196232-dynamic-urls-how/#findComment-1030573 Share on other sites More sharing options...
Bikkebakke Posted March 23, 2010 Author Share Posted March 23, 2010 Got it working, a thousand thanks. Quote Link to comment https://forums.phpfreaks.com/topic/196232-dynamic-urls-how/#findComment-1030590 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.