Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Dynamic page generation stumper



3:33 pm on May 23, 2005 (gmt 0)

10+ Year Member

I've read the charter and combed through the stuff at apache.org, but haven't been able to figure out how to accomplish the following, or even tell if it's possible. Any help is appreciated -

I have a database-driven website consisting of a template.php page and an index.php page. The navigation for the site is pulled from the database and pages are created on-the-fly once requested.

The way this works is, the index page populates with nav items (drawn from MySQL) - when you click on a nav item the new page pulls the appropriate content and populates the template.php page. I'm using mod rewrite to convert the name of the template.php page to /nav-name/index.php

This is the line I do this with:
RewriteRule ^(.*)/index.php(.*)$ /template.php?page=$1

So with 10-pages of content in the SQL table, there's an index page in the root folder, and 9 more sub-folders with index pages of their own, which are really just mod-rewritten template.php pages.

What I'd LIKE to do is, instead of generating the pages in a folder (/nav-name/index.php) I'd like to keep them in the root folder - www.example.com/nav-name.php and still be able to populate the template page with the appropriate content.

Is this possible to do using mod rewrite, and if so - how?


3:57 pm on May 23, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Change the code in template.php to create 'pages' using '/nav-name.php' instead of '/nav-name/index.php'. You can hard-code this or use preg_replace if the pagenames are coming directly out of the SQL data.

Then modify your rewriterule to change requests for '/nav-name.php' to '/template.php?page=nav-name' instead of rewriting '/nav-name/index.php' to '/template.php?page=nav-name'. You will have to add an exclusion for template.php itself, so that it doesn't get rewritten itself, creating an infinite loop -- See RewriteCond %{REQUEST_URI} to get started on the exclusion part.

Once you accomplish this, you'll probably want to redirect all of your old URLs to the new scheme. That's another subject, and one you should probably ignore until you get the main problem worked out.



7:24 pm on May 24, 2005 (gmt 0)

10+ Year Member

That worked - thanks for the tip. For anyone interested, here's what I ended up using:

RewriteCond %{REQUEST_URI}(space)!^template.php
RewriteRule ^(.*).php /template.php?page=$1

Leave a space where (space) is...


12:30 am on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Now, bolt on the turbocharger:

RewriteCond $1 !^template$
RewriteRule ^([^.]+)\.php$ /template.php?page=$1 [L]

(Just makes the patterns more specific, which speeds up processing. The pattern ".*" should be avoided anywhere but at the end of a pattern.)



Featured Threads

Hot Threads This Week

Hot Threads This Month