Creating dynamic directories for your website

Creating dynamic directories for your website

Note: When using this, since you are utilizing a 404 redirect, search engine spiders may be blocked to crawl your content. I use this merely to make it easier for my users to access their content on my pages. Example: http://www.feelthefunk.com/left-4-dead-hints-and-tips

Do you ever notice sites with URLs such as:

http://www.figured-it-out.com/how-to-create-dynamic-directories

Where the URL basically explains the sites contents. When seeing this, I knew that the directories werent being physically created for sites with many news/blog entries.

So this is how you create dynamic directories on an apache server using PHP/MYSQL running Linux.

I have a basic hosting account with Lunarpages.

  1. I created an .htaccess file in the root directory of my website. "public_html"
  2. Inside of the .htaccess file I wrote ErrorDocument 404 /page404.php
  3. What the line above does is basically directs users to a page in the event of a 404 error, a 404 error is when the page a user attempts to access does not exist.
  4. So basically if a user types for example: www.figured-it-out.com/blahblah, the script above will execute and take them to page404.php
  5. Now inside of page404.php, I put the following :

    In your PHP page:
    $referringPage = $_SERVER['REQUEST_URI']
    $pageDir = split("/",$referringPage);

  6. Now your next steps will vary.
  7. I took the $pageDir variable, which is an array, which would have a value of "blahblah" in its last element based on our example above.
  8. $404text = $pageDir[sizeof($pageDir)-1]; // the last element, would contain blah blah
  9. With that variable, I query a "directoryname" column in some table in my MYSQL database. If there is a match, I pull the info from the database and load a page, if there is no page, I load a standard 404 page. Again this is my implementation, but from here you will have the tools to do what you would like.
  10. IE NOTE: Add a few paragraphs of latin or dummy text and comment it out. For IE to load custom 404 pages the 404 page needs to be greater than 500 bytes. But that still didnt work for me so adding a few paragraphs of latin made it a few kilobytes and solved my issue
  11. Add the following to the top of your PHP page, this allows your 404 page to be loaded by IE effectively:

    ob_start();
    header("HTTP/1.1 404 Not Found");
Hope that helps, please feel free to post questions in the comments area below if you need more help on this.

Keywords: create SEO dynamic directories


Submit a Comment