Main Content
Using a MySQL database to control mod_rewrite via PHP Archive - Originally posted on "The Horse's Mouth" - 2007-10-06 00:08:23 - Graham Ellis
Question: How can I set up Apache httpd / mod_proxy to use a program rather that a list of URL patterns to control my rewrites?
Specify a rewrite map type prg in your httpd.conf or .htaccess file. For example:
RewriteEngine On
RewriteMap tryme prg:/home/trainee/website/andy
RewriteRule (.*\.htm) ${tryme:}
Question: How can I write that program in PHP
1. Start the script with a line such as
#!/usr/local/bin/php
or a pointer to whereever the cli version of PHP is located.
2. Ensure you have a
set_time_limit(0);
at the top of that code to ensure it won't time out
3. Open your STDIN for interactive read within the PHP:
$keyboard = fopen("php://stdin","r");
4. Write a daemonic loop to read from keyboard, translate as appropriate, and printg out the mapped URL.
When you restart you httpd with the newly configured httpd.conf, your mapping should be in place. Well - it works for me ;-) but you do need to watch things like buffering!
Question: Can that program access a MySQL database?
Yes - just add in a mysql_connect or mysqli_connect at the top of your code, and appropriate database queries further down, and that's all there is too it. Remember you may need to "bounce" your web server to restart the rewrite daemon.
Putting all the answers together?
There's source code showing most of this ... a fully working example .. here
Some other articles
S156 - Interfacing Applications to MySQL Databases Accessing a MySQL database from Python with mysql.connector MySQL, MySQLi, PDO or something else - how best to talk to databases from PHP Needle in a haystack - finding the web server overload Perl - database access - DBD, DBI and DBIx modules How to display information from a database within a web page Joining a MySQL table from within a Python program Connecting Python to sqlite and MySQL databases Checking the database connection manually Mysqldump fails as a cron job - a work around Hiding a MySQL database behind a web page Uploading to a MySQL database through PHP - examples and common questions Downloading data for use in Excel (from PHP / MySQL) Easy selection of multiple SQL conditions from PHP This article Viewing images held in a MySQL database via PHP Python to MySQL Using a MySQL database from Perl mysql_connect or mysql_pconnect in PHP? H115 - Designing PHP-Based Solutions: Best Practice Real life PHP application using our course training MVC example Using an MVC structure - even without a formal framework Learning to program - comments, documentation and test code We not only teach PHP and Python - we teach good PHP and Python Practice! Even early on, separate out your program from your HTML! Filtering PHP form inputs - three ways, but which should you use? PHP sessions - a best practice teaching example Injection Attacks - PHP, SQL, HTML, Javascript - and how to neutralise them Separating program and artwork in PHP - easier maintainance, and better for the user How to build a test harness into your PHP Not just a PHP program - a good web application Adding a newsfeed for your users to a multipage PHP application Improving the structure of your early PHP programs Refactoring - a PHP demo becomes a production page Defensive coding techniques in PHP? PHP Techniques - a workshop Short and sweet and sticky - PHP form input Software to record day to day events and keep an action list Efficient PHP applications - framework and example A story about benchmarking PHP Ordnance Survey Grid Reference to Latitude / Longitude Converting from postal address to latitude / longitude Controlling and labelling Google maps via PHP This article Easy handling of errors in PHP Resetting session based tests in PHP Drawing hands on a clock face - PHP Painting a masterpiece in PHP Good Programming practise - where to initialise variables Back button - ensuring order are not submitted twice (PHP) Learning to write secure, maintainable PHP Maintainable code - some positive advice Code quality counts Global, Superglobal, Session variables - scope and persistance in PHP PHP - good coding practise and sticky radio buttons Reporting on the 10 largest files or 10 top scores Giving the researcher power over database analysis Merging pictures using PHP and GD Robust checking of data entered by users A year on - should we offer certified PHP courses Code and code maintainance efficiency Putting a form online Crossfertilisation, PHP to Python Short underground journeys and a PHP book A607 - Apache httpd mod_rewrite Forwarding a whole domain, except for a few directories - Apache http server Adding a passcode to a directory Telling which ServerAlias your visitor used - useful during merging domains Simplest ever proxy configuration? Finding and diverting image requests from rogue domains How to set up short and meaningfull alternative URLs Redirecting a page - silent, temporary or permanent? Dynamically watching your web site via a PHP wrapper Carrying a long URL around - looking for memorable shorts Redirecting a home page using mod_rewrite Bookkeeping If you have a spelling mistake in your URL / page name mod_rewrite for newcomers Pointing all the web pages in a directory at a database More HowTo diagrams - MySQL, Tomcat and Java Apache httpd, MySQL, PHP - installation procedure What to do if the Home Page is missing This article Choosing between mod_proxy and mod_rewrite Simple but effective use of mod_rewrite (Apache httpd) Passing GET parameters through Apache mod_rewrite Apache httpd and Apache Tomcat together tips Clustering, load balancing, mod_rewrite and mod_proxy Using different URLs to navigate around a single script Apache httpd to Tomcat - jk v proxy A603 - Further httpd Configuration Identifying and clearing denial of service attacks on your Apache server Helping search engines with appropriate 400 error codes Building up from a small PHP setup to an enterprise one Forwarding a whole domain, except for a few directories - Apache http server Parse error: parse error, unexpected T_STRING on brand new web site - why? Apache Internal Dummy Connection - what is it and what should I do with it? An image from a website that occasionally comes out as hyroglyphics Redirecting a page - silent, temporary or permanent? How did I do THAT? Monitoring and loading tools for testing Apache Tomcat Database connection Pooling, SSL, and command line deployment - httpd and Tomcat Moving a directory on your web site How to avoid duplicating web page maintainance mod_rewrite for newcomers mod_proxy_ajp and mod_proxy_balancer examples Pointing all the web pages in a directory at a database mod_proxy and mod_proxy_ajp - httpd WEB-INF (Tomcat) and .htaccess (httpd) Configuring Apache httpd What to do if the Home Page is missing User and Group settings for Apache httpd web server Strange behaviour of web directory requests without a trailing slash Default file (MiMe types) for Apache httpd and Apache Tomcat Online hotel reservations - Melksham, Wiltshire (near Bath) Which modules are loaded in my Apache httpd This article Load Balancing with Apache mod_jk (httpd/Tomcat) .php or .html extension? Morally Static Pages Compressing web pages sent out from server. Is it worth it? Simple but effective use of mod_rewrite (Apache httpd) Sharing the load with Apache httpd and perhaps Tomcat httpd.conf or .htaccess? Passing GET parameters through Apache mod_rewrite Clustering, load balancing, mod_rewrite and mod_proxy To list a directory under httpd on a web server, or not? Using different URLs to navigate around a single script An unhelpful error message from Apache httpd Denial of Service ''attack'' Apache httpd to Tomcat - jk v proxy 2006 - Making business a pleasure Apache httpd - serving web documents from different directories Separating 'per instance' data from binaries and web sites Spotting a denial of service attack