Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite question concerning dynamic page generation

can it handle creating the onpage links as well?

         

David Bruning

12:03 am on Feb 10, 2005 (gmt 0)

10+ Year Member



In a nutshell, here's the situation.

18,000 entrees in a database.
say 10 entrees to be listed per page.

so, 1800 pages.

I don't want to hand code these naturally if i can help it.
(this is for my pet sports project and I can only work on it at night)

is there a way to have mod rewrite create static .html pages based on a couple of fields in the database, AND handle creating the links in the pages so they look static?

I have spent some time looking at mod rewrite, but I am not sure this will do what I want. :P

Should I just make a php template and get to work, or do you think mod rewrite will be my savior?

(not looking for just answers, opinions on whether this is worth pursuing would be appreciated too :)

jdMorgan

1:21 am on Feb 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mod_rewrite can't help with the on-page links. mod_rewrite is activated after receipt of a request on your server, and before the content-handler is invoked to serve a static page or run a script that will serve a dynamic page (See API phases in the Apache mod_rewrite documentation).

mod_rewrite functions as an incoming-URL to filename translator (internal rewrite mode), or as an incoming-URL to URL translator (external redirect mode).

So yes, you'll have to change the links that are output as HTML on your pages. When someone clicks on one of those 'static' links, mod_rewrite can intercept that URL and rewrite it to your script, moving part of the requested URL-path into the script URL's query string if desired.

Jim

David Bruning

5:21 am on Feb 10, 2005 (gmt 0)

10+ Year Member



Thank you for the reply :)

Ok, so say I create an index.php, and that within the page, I make a proper link to a static page

ie: <a href="product2135.php">

If I understand you correctly, mod rewrite could take my call of product2135.php (which doesn't actually exist), evaluate it as index.php?product=2135 and display a page that would appear to SE spiders and visitors AS product2135.php?

jdMorgan

4:57 pm on Feb 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes,

Take a look through some of the previous threads here for implementation details. Usually, you can do this using preg_replace in the php file, thus requiring no change to your database. The preg_replaced friendly URL is then "published" on the page, so this is what the world sees. Then, when that friendly URL is requested from your server, mod_rewrite changes it back into the form your script needs, and none the wiser. Preg_replace and mod_rewrite both use regular expressions, and what preg_replace does to the orignal URL, mod_rewrite reverses. SO the outside world sees a friendly URL, and the internal operation of your script is unchanged.

Try using a Google site search of WebmasterWorld for "rewrite static URL dynamic" and similar phrases - there are a lot of threads on the subject.

Jim

David Bruning

6:05 pm on Feb 10, 2005 (gmt 0)

10+ Year Member



Alrighty, I am going to give this a shot ;)

Everything is pretty much ready, 1st I'll create the regular php/dynamic scripts that will display everything as usual.

Then I will try to tackle the .htaccess side of it :)

Wish me luck!

David Bruning

11:04 pm on Feb 10, 2005 (gmt 0)

10+ Year Member



oh glorious guru's of wisdom, my brain hurts :)

Say i have a folder off the root called widget
and say what i am looking for is to call:

[domain.com...]

as have it parsed, etc,etc as:

[domain.com...]

Here's what I have:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /widget
RewriteRule /(.*)/(.*)\.php$ /index.php?$category=$1\&$startdisplay=$2

And yet, no happiness.

I plan to keep trying to see where my mistake is, but if anyone would like to learn me....i'd appreciate it :)

David Bruning

11:46 pm on Feb 10, 2005 (gmt 0)

10+ Year Member



scratch that, it should have been:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /posters
RewriteRule ^(.*)/(.*)\.php$ index.php?$category=$1\&$startdisplay=$2 [R=301,L]

now to test and polish it :)
Any code tightening people notice, please let me know.

David Bruning

12:37 am on Feb 11, 2005 (gmt 0)

10+ Year Member



final version:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /posters
RewriteRule ^(.*)/(.*)\.php$ index.php?category=$1\&displaypage=$2 [L]

I simply cannot believe the power this has.

I kind of grasped it yesterday. Today I am in shock.

almost 2000 static pages ready for the search engines.

It took 2 files, taking just over 22k in harddrive space (totally ignoring the database size of course)

Amazing. Simply Amazing. I plan on reworking several major section of our websites using this technology :)

jdMorgan

2:10 am on Feb 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try to avoid using the pattern ".*", especially multiple times in one pattern. ".*" is the most ambiguous possible pattern, and "*" is the greediest operator. So, using ".*/.*" can lead to unexpected problems unless there is only one slash in the URL. That pattern can also cause mod_rewrite to 'retry' many, many times to get a match while processing a pattern. The following rule will be faster:

RewriteRule ^(.*)/([^/.]+)\.php$ index.php?category=$1&displaypage=$2 [L]

Also, note that you don't need to escape characters in the substitution URL; Only the special characters in the regex patterns need to be escaped.

Jim

David Bruning

4:54 pm on Feb 11, 2005 (gmt 0)

10+ Year Member



Jim,

Your suggestion is now in place :)

Thank you for your help in this, it is VERY appreciated :)