用PHP写一个.htaccess文件?

时间:2022-01-09 07:17:37

On my PHP site I would like to sometimes show a maintenance page to users. I know I can do this in PHP very easily but I would rather use an htaccess file, I think performance may be better this way. So what I am wanting to know is can I modify a htaccess file in PHP? If it is possible I am thinking either,

在我的PHP网站上,我想有时向用户显示维护页面。我知道我可以很容易地在PHP中执行此操作,但我宁愿使用htaccess文件,我认为这种方式可能会更好。所以我想知道的是我可以在PHP中修改htaccess文件吗?如果有可能我也在想,

1 - Have to files and 1 will have the code below in it and the other will not, both files would contain my other htaccess stuff as well. Then php can rename these 2 files when I run an admin script to switch which file is shown, by changing it's name to .htaccess

1 - 必须有文件,1将有下面的代码,而另一个不会,两个文件也包含我的其他htaccess东西。然后,当我运行管理脚本来切换显示哪个文件时,php可以重命名这两个文件,方法是将其名称更改为.htaccess

2- Read the contents of the current .htaccess file and APPEND the code below into it, then also be able to remove it from the file if needed.

2-阅读当前.htaccess文件的内容并将下面的代码APPEND到其中,然后如果需要也可以将其从文件中删除。

RewriteEngine On
RewriteBase / 
RewriteCond %{REQUEST_URI} !^/maintenance\.php$
RewriteRule ^(.*)$ http://domain.com/maintenance.html [R=307,L]

My goal is to build an admin script that can have 1 click processing to change the maintenance page to show or not to show.

我的目标是构建一个管理脚本,可以通过1次单击处理来更改要显示或不显示的维护页面。

Please any sample code or tips on how to do this or if it is even possible to modify a .htaccess file from PHP

请提供有关如何执行此操作的示例代码或提示,或者是否可以从PHP修改.htaccess文件

3 个解决方案

#1


12  

You can use fopen to open and fwrite to write to a file. You'll want to open it in append mode, read the manual to see which applies.

您可以使用fopen打开并写入以写入文件。您需要在追加模式下打开它,阅读手册以了解哪种适用。

That said, this sounds like a strange way to go about doing things.

也就是说,这听起来像是一种奇怪的做事方式。

I accomplish what you're trying to do using just rewrite rules that check for file presence:

我只使用重写规则来检查文件存在,从而完成了你要做的事情:

RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /maintenance.html [L]

If the maintenance.html file is present, then my site is under maintenance.

如果maintenance.html文件存在,那么我的网站正在维护。

#2


9  

I would strongly recommend not writing .htaccess from PHP. You would have to give the web server user (who traditionally has very low privilege) write access to .htaccess; all you need is one exploitable script and an attacker could be effectively re-configuring your server.

我强烈建议不要从PHP编写.htaccess。您必须为Web服务器用户(传统上具有非常低权限的用户)提供对.htaccess的写访问权限;您只需要一个可利用的脚本,攻击者可以有效地重新配置您的服务器。

For an in-app-switches maintenance mode stick to having PHP itself return the maintenance page. When you are doing maintenance on the app itself it is easy enough to temporarily rename a file into .htaccess's position to enable the maintenance rewrite.

对于应用程序内切换维护模式,坚持让PHP本身返回维护页面。当您对应用程序本身进行维护时,可以很容易地将文件临时重命名为.htaccess的位置以启用维护重写。

#3


0  

RewriteEngine On
RewriteBase / 
RewriteCond %{REQUEST_URI} !^/maintenance\.php$
RewriteRule ^(.*)$ http://domain.com/maintenance.html [R=307,L]

#1


12  

You can use fopen to open and fwrite to write to a file. You'll want to open it in append mode, read the manual to see which applies.

您可以使用fopen打开并写入以写入文件。您需要在追加模式下打开它,阅读手册以了解哪种适用。

That said, this sounds like a strange way to go about doing things.

也就是说,这听起来像是一种奇怪的做事方式。

I accomplish what you're trying to do using just rewrite rules that check for file presence:

我只使用重写规则来检查文件存在,从而完成了你要做的事情:

RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /maintenance.html [L]

If the maintenance.html file is present, then my site is under maintenance.

如果maintenance.html文件存在,那么我的网站正在维护。

#2


9  

I would strongly recommend not writing .htaccess from PHP. You would have to give the web server user (who traditionally has very low privilege) write access to .htaccess; all you need is one exploitable script and an attacker could be effectively re-configuring your server.

我强烈建议不要从PHP编写.htaccess。您必须为Web服务器用户(传统上具有非常低权限的用户)提供对.htaccess的写访问权限;您只需要一个可利用的脚本,攻击者可以有效地重新配置您的服务器。

For an in-app-switches maintenance mode stick to having PHP itself return the maintenance page. When you are doing maintenance on the app itself it is easy enough to temporarily rename a file into .htaccess's position to enable the maintenance rewrite.

对于应用程序内切换维护模式,坚持让PHP本身返回维护页面。当您对应用程序本身进行维护时,可以很容易地将文件临时重命名为.htaccess的位置以启用维护重写。

#3


0  

RewriteEngine On
RewriteBase / 
RewriteCond %{REQUEST_URI} !^/maintenance\.php$
RewriteRule ^(.*)$ http://domain.com/maintenance.html [R=307,L]