gorgo666 Posted July 23, 2008 Share Posted July 23, 2008 Hello everyone. I've beenputting off joining phpfreaks for a lonnggg time but here I am, finally! But anyway. I have a question that I've been pondering for ages, and it may be a little obvious so please don't ridicule me. Most websites now-a-days seem to have completely removed file names from their addresses and navigation. To give you an example, a lot of people may have used http://www.acme.com/about.php but now instead you only see http://www.acme.com/about/ which obviously refers to a directory. It looks much tidier and is really Web 2.0... Can someone elaborate on this? Is it a behind the scenes CMS doing this, like Drupal or Wordpress? Or is it simply people storing an index.php file inside that directory? I look forward to any reply! Cheers guys. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 23, 2008 Share Posted July 23, 2008 Research mod_rewrite That's what you'll need to be using, and it's not PHP related. And check out this board on this very website, http://www.phpfreaks.com/forums/index.php/board,50.0.html Good luck. Quote Link to comment Share on other sites More sharing options...
gorgo666 Posted July 23, 2008 Author Share Posted July 23, 2008 Thanks for the very quick reply. I always thought it was something to do with mod-rewrite. I thought the default mod-rewrite code that Wordpress gives you does the trick, or Zend Framework, as that seems to remove index.php from the end of the URL...? I always thought that until I just tried making a directory for each section of the site such as "site_root/about" and then inside the about directory, I would create an index.php file and everything worked as planned... Out of interest, how would you go about doing this? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 23, 2008 Share Posted July 23, 2008 Okay, well you don't need any directories. You just need the normal pages, so about.php, contact.php etc etc. Then enable rewrite_module, which is an Apache Module. This allows you to, of course, use mod_rewrite. Now you need to create a new file and call it .htaccess In this file we will put rules which will make things like www.website.com/about/ actually point to www.website.com/index.php?page=about That's all mod_rewrite does. So the user may think they are in the directory "about" but in fact they are still in the root directory. So, in the .htaccess file put this: RewriteEngine on RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L] That's it, that will turn www.website.com/about/ into index.php?page=about So then your index.php page you can include the about.php page. Of course that will only work for 1 directory, you need to add more rules if you want links like www.website.com/about/history/ Good luck. Quote Link to comment Share on other sites More sharing options...
gorgo666 Posted July 23, 2008 Author Share Posted July 23, 2008 Very smart. I'll have to have a sit and play later with a coffee. Cheers for that. I'm guessing once you've passed the action through GET with mod_rewrite, a simple case or if statement would work to figure out the user's target URL from the page variable... Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 23, 2008 Share Posted July 23, 2008 Yup, you just retrieve the variable like you normally would. So your navigation would have a link like: <a href="/about/">About Us</a> Then in the content area of your index.php you would have: $page = $_GET['page']; //$_GET['page'] was set with mod_rewrite include($page.".php"); Or something similar. Quote Link to comment Share on other sites More sharing options...
gorgo666 Posted July 23, 2008 Author Share Posted July 23, 2008 Thanks for the reply! Sorry about the double post by the way, was an accident. I've tried this out but it doesn't seem to be working. I wrote some PHP first to test it, at a very basic level: <?php /** PHP BOOTSTRAP FILE ****************** Provides the include to the relevant URI based on the parameter **/ if ($_GET['page']) { $page = $_GET['page']; } else { $page = "home"; } // If nothing passed, then give the home page echo($page . ".php"); ?> As you can see, if you type http://test.dev/?page=about, the browser echos out "about.php". If you don't specify anything, "home.php" is echoed. I also made a .htaccess file in the directory with this index.php file (I'm using Mac so it's ~/Sites/test.dev), and in htaccess I put: RewriteEngine on RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L] But instead when I put http://test.dev/about, I simply get a "page cannot be found" error, so I don't think the rewrite is working at all. What have I done wrong? Have I put the htaccess file in the wrong place? Thanks for your help ProjectFear. Quote Link to comment Share on other sites More sharing options...
gorgo666 Posted July 23, 2008 Author Share Posted July 23, 2008 Problem solved. On a Mac 10.5.4 Leopard, you have to update the config file in /etc/apache2/users/fredbloggs.conf to: <Directory "/Users/fredbloggs/Sites/"> Options All AllowOverride All Order allow,deny Allow from all </Directory> Hope this helps anyone with a similar issue. Quote Link to comment 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.