用PHP编写.htaccess文件? [英] Write a .htaccess file in PHP?

查看:189
本文介绍了用PHP编写.htaccess文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的PHP的网站,我想有时显示维护页面给用户。我知道我可以在PHP中很容易做到这一点,但我宁愿使用一个htaccess的文件,我想表现可能会更好这样。那么,我想知道的是我可以修改PHP中的htaccess的文件?如果有可能,我想要么,

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,

1 - 有文件和1将低于它的code和对方不会,这两个文件将包含我的其他htaccess的东西,以及。然后,当我运行的管理脚本,通过改变它的名字为.htaccess切换显示的文件,PHP可以重命名这些文件2

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

2 - 读取当前.htaccess文件的内容,并加入下面的code进去,然后还可以从文件中删除,如果需要的。

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.

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

我的目标是要建立一个管理脚本,可以有1击处理来改变维护页面显示或不显示。

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.

请任何样品code或如何做到这一点,或者如果它甚至有可能修改从PHP .htaccess文件

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

推荐答案

您可以使用的fopen 开和的 FWRITE 以写入文件。你会想打开它附加模式,阅读手册,看​​看哪些应用。

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.

不过,这听起来像一个奇怪的方式去做事。

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

我完成你想只使用重写检查文件$ ​​P $ psence规则做:

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]

如果该maintenance.html文件是present,那么我的网站正在维护中。

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

这篇关于用PHP编写.htaccess文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆