Can I make a dynamic .htaccess file?

后端 未结 3 2105
自闭症患者
自闭症患者 2020-12-30 17:14

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

相关标签:
3条回答
  • 2020-12-30 17:41

    You definitely can generate your .htaccess with PHP if you had a backend script to do it - say in your admin area. But it seems risky and potentially unnecessary? if something goes wrong, your site will be down until you fix it - that potentially means the page you're using to generate it. Why not just create an interface that generates the new rules for you from a database, then you can have a quick look over and copy them in?

    What exactly is it that you're trying to update?

    0 讨论(0)
  • 2020-12-30 18:00

    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.

    0 讨论(0)
  • 2020-12-30 18:00

    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).

    0 讨论(0)
提交回复
热议问题