我可以制作动态 .htaccess 文件吗?

发布于 2024-09-24 23:49:20 字数 116 浏览 0 评论 0原文

我可以使用 php 生成 .htaccess 吗?
我想使用 php 根据数据库中的信息动态创建我的 htaccess 文件。
每当我更改一些代码时,它都会省去制作新的 .htaccess 文件的麻烦。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深居我梦 2024-10-01 23:49:20

您可以使用文件系统函数来读写文件,例如:

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

但是如果您使用一条静态规则将请求重定向到 PHP 文件,然后由 PHP 文件执行其余操作。

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.

燃情 2024-10-01 23:49:20

你可以吗?当然。只要您有权限,您就可以使用 PHP 在文件系统中创建文件。

你应该吗?好吧,如果您仅在“命令行”模式下使用 PHP,我认为它与其他脚本语言一样好。如果您从网站上执行此操作,出于安全原因,我会说这是一个坏主意。另一种技术可能更合适(如果您最初有 XML,那么您会想到 XSLT,但您使用的是 mysql,因此您需要一些其他类型的脚本)。

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

清醇 2024-10-01 23:49:20

如果您有后端脚本来执行此操作,那么您绝对可以使用 PHP 生成 .htaccess - 例如在您的管理区域中。但这似乎有风险并且可能没有必要?如果出现问题,您的网站将关闭,直到您修复它 - 这可能意味着您用来生成它的页面。为什么不创建一个界面来从数据库中为您生成新规则,然后您可以快速查看并将它们复制进去?

您到底想更新什么?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文