views:

51

answers:

2

Can I make my .htaccess be generated with php?
I would like to use php to dynamicly create my htaccess file from information from the database.
It would save me the trouble of making a new .htaccess file whenever I change a bit of my code.

+1  A: 

You can read and write files with the file system functions, for example:

$data = <<<EOF
RewriteEngine on
RewriteRule …
EOF;
file_put_contents('.htaccess', $data);

But it would be more flexible if you use one static rule that redirect the requests to your PHP file that then does the rest.

Gumbo
Hm, i have a bunch of rewrites, that rewrite to my index.php with diffrent get vars
InsanityNet
Are you sure you can't handle this rewrite with one general rule, then check for a valid get variable (which you need to do anyway for security) within index.php?
Andrew
This is the contents of my current .htaccess http://pastey.net/140995
InsanityNet
@InsanityNet: So?
Gumbo
Im showing that diffrent pages require a dedicated rewrite.
InsanityNet
This answer actully makes most sense, as I can make a cron job to gen the .htaccess
InsanityNet
@InsanityNet: I was rather thinking of a PHP variant of this routing. When using a uniform, hierarchical URL design, you can easily pass the request to the corresponding scripts.
Gumbo
Yes, but I also like using clean urls.
InsanityNet
@InsanityNet: They are not mutually exclusive. In fact, the application (PHP) does know what the URLs mean better than your web server (.htaccess). So it should be the application that does the request parsing and not the web server. That would also eliminate writing rewrite rules dynamically.
Gumbo
+1  A: 

Can you? Sure. You can create files in the filesystem with PHP so long as you have permissions.

Should you? Well, if you are only using PHP in "command-line" mode, I suppose it's as good as another scripting language. If you are doing so from a website, I would say this is a bad idea for security reasons. Another technology might be more appropriate (if you originally had XML, then XSLT would come to mind, but you're using mysql, so you'll need some other kind of script).

Andrew
How does this help my question? You wouldn't use .htaccess in command line mode.....
InsanityNet
No, but that isn't stopping you from generating it, just as you might with some other script. If you're wanting to generate it *from a web page call*, that's a different matter.
Andrew
No, I would like to basicly tell apache to call .htaccess (like it does), parse it in php, _then_ parse it in apache
InsanityNet