views:

420

answers:

5

Hey everyone,

Im developing a new site, and I'd like to store my rewrite rules in a database, instead of right in the .htaccess files.

I have another site that uses Opensef (http://sourceforge.net/projects/opensef/) with a Joomla! installation that is doing this, but im not even 100% how it works underneath the hood.

How can I store these rules in a database, query for them on request and rediret to the clean URL if found? Is there a better way to do this instead of loading up a .htaccess file (there may be 1000's of entries)?

Thank you,

+5  A: 

You can get mod_rewrite to generate a map from external source such as executing a PHP or Python file which can get the data from the database and create a mod_rewrite map.

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html (See right at the bottom)

For example

RewriteMap    quux-map       prg:/path/to/map.quux.pl

Good Luck

Aiden Bell
Good suggestion!
Ryan Rohrer
Keep in mind that this program needs to be FAST -- it's going to run on every request through your web server, even images and CSS files.
Eli
Apache/Rewrite loads the map and caches it until SIGHUP if I recall.
Aiden Bell
A: 

Grab the UrlRewriteFilter, butcher it to use a DB, and use that in Tomcat instead of Apache.

Tomcat is a fine web server and can do many things Apache can do (like FastCGI for PHP), and writing stuff like this for it is trivial compared to writing such things for Apache.

Will Hartung
+1  A: 

I think that the best approach to use rules stored in a database is:

  • Store the rules in your database through your admin panel of your site.
  • Then after updating database, generate a new .htaccess using the rules in DB using your server-side language solution.
  • Replace old .htaccess with new one.

This avoids the server load. It's similar to Aiden Bell solution.

clinisbut
+2  A: 

Assuming all these pages are ultimately in Joomla, I think using .htaccess or mod_rewrite is a mistake.

I think you're much better off learning how Openserf works. I'd imagine it has a little piece of code that runs early on for every request that queries the database and issues a Redirect through PHP if there's a hit. A further advantage of this approach is that it should even be possible to have Joomla rewrite links on its pages to point to the clean version in the first place, saving the user an unneeded redirect

Incidentally, this is how the Pathauto module in Drupal does it, and I use that all the time on some pretty high volume sites with many thousands of pages.

Eli
A: 

What you probably want is a single rewrite rule to handle every unknown request that comes in and then pass that to a small script that will handle the lookups & generate redirects. You could even skip the rewrite rule completely and use the Apache ErrorDocument directive to pass unknown URLS into the script.

You've been pretty slim on the details of what this 'new site' is but, you might want to consider building yourself a Front Controller for the app & having it take care of all the incoming URLs. Many (most?) web app frameworks take this approach.

Sean McSomething