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.
views:
51answers:
2
+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
2010-09-28 19:15:29
Hm, i have a bunch of rewrites, that rewrite to my index.php with diffrent get vars
InsanityNet
2010-09-28 19:17:14
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
2010-09-28 19:18:28
This is the contents of my current .htaccess http://pastey.net/140995
InsanityNet
2010-09-28 19:20:24
@InsanityNet: So?
Gumbo
2010-09-28 19:21:32
Im showing that diffrent pages require a dedicated rewrite.
InsanityNet
2010-09-28 19:22:15
This answer actully makes most sense, as I can make a cron job to gen the .htaccess
InsanityNet
2010-09-28 19:25:20
@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
2010-09-28 19:33:34
Yes, but I also like using clean urls.
InsanityNet
2010-09-28 19:34:37
@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
2010-09-28 19:37:43
+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
2010-09-28 19:17:15
How does this help my question? You wouldn't use .htaccess in command line mode.....
InsanityNet
2010-09-28 19:18:22
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
2010-09-28 19:19:30
No, I would like to basicly tell apache to call .htaccess (like it does), parse it in php, _then_ parse it in apache
InsanityNet
2010-09-28 19:21:36