Forum Moderators: phranque

Message Too Old, No Replies

Fastest way to solve mod-rewrite problem

         

wheel

6:41 pm on May 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've got a website program that uses dynamic URL's. I have a mysql database table of mappings of the dynamic url't to static url's.

What's the best way (and most se friendly) to display the dynmaic url give a statuc url is entered? Unfortunately there's no easy reg expression for either the dynmaic or static url's.

We're currently redirecting 404 error pages (the static url;s) to a php script that looks up and displays the correct page from the database. Unfortunately this method is also returning a 404 to the se's.

jd01

9:11 pm on May 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi wheel,

I think more information is really necessary, before anyone can actually attempt to answer your question. Without knowing your URL structure, what you have tried or are considering trying, and what exactly you mean by 'no easy regex' solution, there is no real way to comment.

Would you please, provide examplified instances of each, and any other information that might be helpful or necessary?

Justin

wheel

9:24 pm on May 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've got a content management system with a variety of modules and calculators. Each module can have multiple instances based on the variables that are passed to it.

So an example of a couple of urls might be:
www.domain.com/module.php&modulename=calculator&page=1
www.domain.com/module.php&modulename=calculator&page=3

or
www.domain.com/othermodule.php&modulename=userpages&page=44

where they map respectively (via a database table) to:
bigcalculator
calculator-ohio
contact-us

when you visit contact-us, the 404 page which is really a php script looks up contact-us in the table and spits out the appropriate dynamic page.

I thought I had this whipped,but it's kicking out a 404 in the page headers when I do it that way. I'm trying to preserve my table functionality while still getting rid of the 404 problem. I've got some functionality built into the website that allows me to do the mapping that I'd really like to preserve. Otherwise I'm going to have to rework the guts of the cms (which I'm guesing I'm going to have to do anyway :) ).

jd01

10:44 pm on May 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi wheel,

It looks like...

You can change these:
www.domain.com/module.php&modulename=calculator&page=1
www.domain.com/othermodule.php&modulename=userpages&page=44

to these:
www.domain.com/modulename/calculator/1.html
www.domain.com/othermodule/userpages/44.html

and use this:
RewriteRule ^modulename/([a-z-]+)/([0-9]+)\.html$ /module.php&modulename=$1&page=$2 [NC,L]
RewriteRule ^othermodule/([a-z-]+)/([0-9]+)\.html$ /othermodule.php&modulename=$1&page=$2 [NC,L]

I will elaborate later, was... just checking in for a few minutes.

Maybe I am missing something?

Justin

BTW this ([a-z-]+) will catch letters or hyphens between / and / so it will qualify for calculator-ohio, etc.

Added: $ to end the left side rule... see what happens when I hurry?

wheel

3:11 pm on Jun 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's not going to work - I can't use regex's.
Is there any way to do mod_rewrite type of functionality using a mysql database?

wheel

8:31 pm on Jun 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just for future note, I got a response from a newsgroup. I've copied the response below. Basically what it does is rewrites anything not found to a script. Haven't tested it yet, but expect it to work.

The books suggests the use of a rewritemap to an external program -or
script- that requests your database and returns the response to Apache:
RewriteMap nice2hash ext:somescriptorprogram
RewriteCond %{ SCRIPT_FILENAME } !-f # for missing files
RewriteRule (.*) ${nice2hash :$1¦$1}

[httpd.apache.org...]
[httpd.apache.org...]
[httpd.apache.org...]

Just keep the 404 status away by _redirecting_ to that script:
rewritecond %{ SCRIPT_FILENAME } !-f
rewriterule .* /your404script.php [PT,L]

jd01

10:42 pm on Jun 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just keep the 404 status away by _redirecting_ to that script:

I would be *very* careful about not ever serving a 404 error...

You run a couple of risks depending on how your script works:
1. duplicate content on what are served as real pages.
2. 1 page with 'ever changing' content based on a request string.

If someone requests a page that really does not exist how does the script respond? If it serves a 'site-map', 'directory' or any other 'standard' page... any broken links will result in duplicate content.

If requests or a portion of requests are passed in any way other than a regular, non-query string, URL, the page will appear to be the same, with changing or rotating content. EG If someone requests stuff/index.html, and that page uses a POST (or even GET) to get more information out of the script, the page will not change in the browser, but the content will, an ever changing page.

Hope it works out for you though.

Justin