Getting htaccess to read from mysql database

Hi there im not sure if this is php related but im wanting to know how can i get an htaccess file coming from a mysql database does anyone know if this is possible if it is how do i go about doing this?

the reason i am asking is i am wanting to work on a dynamic mod re-write using the mysql database i thought this would be a good way to do it.

so does anyone have any ideas on how to achieve this?


You want to dynamically alter the .htaccess file? Would this be an installation procedure or part of the application itself?

If it’s the latter, I’d highly recommend you seek an alternative.

What are you attempting to do?

Your best bet would be to utilise something called a Front Controller, here’s a quick and dirty example.


# Turn on URL rewriting
RewriteEngine On

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule (.*) index.php?$0 [PT,L]



So, given the URL [B][/B], $sSlug would become view/blog/page/5. You can now use this variable as you wish, search a database for a matching page, redirect etc…

this would be part of an application itself

is this possible to have a php code that generates a htaccess file based on entries in a mysql database?

Sure, perfectly possible. Just use the usual file manipulation functions(fwrite, fopen etc…).

I’m unsure what you are trying to achieve here, are you wanting to specify routes of some sort? Maybe with a little background we could suggest a more standard/best practice based solution?

mod_rewrite does support letting you use a seperate script or program to do the url translation. You can also specify for it to use a regular txt file to lookup the rewrite rules, or a dbm type file(optimized binary file).


Keep in mind that RewriteMaps need to be specified in server config.
You can USE them in .htaccess but the must be SPECIFIED in the config (e.g. apache2.conf, httpd.conf etc.)

sounds like an architecture problem

is this a good idea? if your htaccess is screwed up you’d get a 500 error.


Actually, I think having key application stuff, such as url mappings, in a server configuration file is pretty folly. Don’t rewrite urls, route urls.

how do you mean?

You probably could make it write the .htaccess file everytime…


Come on, you don’t really mean that do you? Don’t get me wrong, I’m all for a laugh. I even got married to convince myself I have a life away from my laptop and current favourite IDE.

Just because you could doesn’t mean you should - at least for all the reasons previously mentioned.

Every time he changes it in the database he could re-write it to the file system. Otherwise he could do a custom routing module for his site.

Yea Exactly Darren can that be done?

Giving a web script the write permissions needed to create an .htaccess file can be very dangerous.

how can it be dangerous? is there another way of doing it without using htaccess then?

I’m still trying to figure out any scenario where you would need to rewrite the .htaccess on the fly, or even frequently enough that you would need to pull it from the database.

The reason it could be dangerous is if a hacker can trick your script into writing arbitrary values to the .htaccess file they can get redirects to other directories of your machine.

My own approach doesn’t even use .htaccess since mod_rewrite applied at the httpd.conf layer is 10 to 20 times faster than when applied in htaccess. Apache looks for the file and if it doesn’t find it then it hands off operational control to a PHP framework to resolve the request. The only thing that is unique to my approach is I’m doing it in httpd.conf. Drupal, Joomla et al do the same thing but at the .htaccess level since, like most php applications, they are of a ‘drop this in your public_html folder and be done’ approach.

dJango, in python, assigns itself as a directory handler in Apache, so all requests pass through this. This is a bit slower than letting Apache do it all, but not as slow as things would be if PHP handled all requests (which it isn’t designed to do - python being a compiled language is better equipped for such an approach).

In each of these approaches though the code given to Apache to follow is very small and never changes once set. With that in mind I did write an automation script that can change the httpd.conf values for the framework - but this script isn’t invokable from a web browser and must be invoked through ssh (and further using sudo), and second it is only invoked when I’m adding a new website project to the server, which might only occur once every couple weeks.

From a technical perspective it isn’t too difficult to do this in PHP. .htaccess (and httpd.conf for that matter) are plain text files. A PHP script that opens a connection to the database and parses what it finds into a text file is very easily done - but seriously consider why you’re wanting to do this because it appears to be (on the face of it) to be a very bad approach.

If you change your .htaccess based on user input, one malicious user will find a way to craft an entry that will let him invade your system, or at least bring your site down. Also remember that your site may be accessed while you are rewriting the .htaccess file, so you will probably have sporadic errors.

SilverBulletUK presented a much better solution: you re-route everything to an index.php, and there you analyze the URL. After that, you can include() whatever you want.

This all sounds like something a RewriteMap would be perfect (actually designed) for.