2

for technical reason and test new features on my PHP website I want to disable access usual users to website temporarily and show a message like 'Site is Under Maintenance'.

However I want to be able to access all pages and I have normal usages.
How can I do that with htaccess , php codes or other methods?

Ahmad Badpey
  • 6,348
  • 16
  • 93
  • 159
  • 1
    Just allow your ipaddress to access the page. for example $ip = $_SERVER['REMOTE_ADDR']; if($ip !="yourip"){echo "Site is Under Maintenance";} – jewelhuq Sep 28 '15 at 10:14

3 Answers3

5

You will find answer here:

Deny all, allow only one IP through htaccess

or you can do it by PHP also:

if your own ip is : 10.233.34.12

if($_SERVER['SERVER_ADDR'] != '10.233.34.12'){
  header("Location: http://example.com/siteUnderMaintenance.php");
 die();
}
Community
  • 1
  • 1
bhushanRJ
  • 164
  • 1
  • 11
3
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1 //your IP
RewriteCond %{REMOTE_ADDR} !^1\.11\.11\.111 //your server IP
RewriteCond %{REQUEST_URI} !^/soon\.html$
RewriteCond %{REQUEST_URI} !^/logo\.jpg$
RewriteRule ^(.*)$ /soon.htm [R=301,L]

Use this in .htaccess

Gaurang Joshi
  • 684
  • 16
  • 32
2

Allow only your IP. Try to put it in your .htaccess

order deny,allow
deny from all
allow from 111.222.333.444

Or you could use

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

The above code sends all users to maintenance.html EXCEPT those with the specified IP.

Harshit
  • 5,147
  • 9
  • 46
  • 93
  • and if OP has apache 2.4 or later? – hjpotter92 Sep 28 '15 at 10:15
  • how can I show a message or redirect users to construction page in this method? – Ahmad Badpey Sep 28 '15 at 10:16
  • @ahmad, Blocked visitors will be shown a '403 Forbidden' error message. You can customize this error message by updating 'Error Documents'. `ErrorDocument 403 /error_pages/403.html` – Harshit Sep 28 '15 at 10:21
  • @Harshit Shrivastava, I try your first code in htaccess along to `ErrorDocument` on localhost and create 403.html page on the root like this : `ErrorDocument 403 /403.html order deny,allow deny from all allow from 31.58.64.19` , however 31.58.64.19 is my ip, I can not open pages and shown *Forbidden* default message. – Ahmad Badpey Sep 28 '15 at 10:42