7

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 - 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- 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]

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.

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

2
  • us2.php.net/manual-lookup.php?pattern=write+file fwrite is the 4th hit Commented Jan 19, 2010 at 0:10
  • 1
    @the down-voters, obviously I do not know how to do this or I would have not "wasted my time writing this" so thanks for nothing =) Commented Jan 19, 2010 at 0:17

4 Answers 4

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.

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I wasn't sure if this was some sort of protected file or not, I don't really have much experience working with files.
I agree with the solution here. I know programs like Cpanel and Wordpress will write to / autogenerate .htaccess code. But it seems like a ten-ton hammer for something that's easily done another way.
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.

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.

Comments

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

Comments

0
Options -MultiViews
RewriteEngine On

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]

RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

<Files ~ "^.*\.([Ee][Nn][Vv])">
 order allow,deny
 deny from all
 satisfy all
</Files>

what is the error it this page, it doesnt allow me to proceed to next page

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.