37

Basically, I am trying to work on the front end of a website, but I would like everyone else but myself to be redirected to a construction page if you like. I currently have:

redirect 301 /index.php http://www.domain.com/construction.php

While this works, it works to well, I would like to be able to still see the live site myself, is it possible to exclude everyone but my IP?

Thanks again.

Oldskool
  • 34,211
  • 7
  • 53
  • 66
Aaron Lee
  • 1,146
  • 3
  • 14
  • 29

10 Answers10

74

You could do it with mod_rewrite

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteRule index.php$ /construction.php [R=301,L]
Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
kufi
  • 2,418
  • 19
  • 14
  • 2
    What about adding IP's, like a list of IPs? – Ben Racicot Apr 18 '14 at 23:09
  • @BenRacicot: Try something like this: http://stackoverflow.com/questions/11653461/redirect-a-range-of-ips-using-rewritecond – kufi May 28 '14 at 06:50
  • What about a background image on construction.php? That would be redirected as well, how to fix that? – riseagainst Jun 30 '14 at 16:31
  • 1
    2 notes: this will only redirect requests to "index.php" not to any other files and you may also need to include conditions to not redirect for any files you reference in the construction page. Thus @linuskleen's answer works better http://stackoverflow.com/a/8985628/5441 – Darryl Hein Nov 14 '15 at 19:22
34

You'll need some conditions before redirecting:

RewriteCond %{REMOTE_ADDR} !=1.3.3.7
RewriteCond %{REQUEST_URI} !=/construction.php
RewriteRule .* /construction.php [L]

Also, to make sure after the lock-out is removed, clients will see the actual page, this solution does not redirect clients permanently (using a 301 redirect), but internally redirects. Substitute 1.3.3.7 for the actual IP address you're using.

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
6

If your apache version is 2.4* , You can redirect your visiters to construction page using the following directives in htaccess :

<If "%{REMOTE_ADDR} !='yourIp'">
RedirectMatch ^/((?!construction.php).*)$ /construction.php
</If>

It says if the ip address is not yourIp redirect all requests to /construction.php .

On older versions of apache, you can use the following mod-rewrite based solution :

RewriteEngine on

RewriteCond %{REMOTE_ADDR} !^myIP$
RewriteRule !construction\.php /construction.php [L]

This internally forwords the request to /construction.php if the RewriteCondition meets. You can Replace L with R if you want to see the redirected url in browser address bar.

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
3

If you have a range of IPs you want to exclude from seeing 'under construction' page you can use |

RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^127.0.0.1|212.250.141.228
RewriteRule ! construction\.html /construction.html [R]

It is important to put the 2 last lines at the end of your .htaccess file, especially when it contains more rewriting rules.

Adam Bubela
  • 9,433
  • 4
  • 27
  • 31
3

hi there you could do the following in .htaccess file

RewriteEngine on
# Redirect all except allowed IP
RewriteCond %{REMOTE_ADDR} !^12.345\.678\.901$
RewriteRule /index.php http://www.domain.com/construction.php [R=302,L]

putting your IP instead of 12.345.678.901

olly_uk
  • 11,559
  • 3
  • 39
  • 45
1

The following worked for me

Deny from all
Allow from xxx.xxx.xx.xxx 
Shairyar
  • 3,268
  • 7
  • 46
  • 86
0

In addition to using the if directive as other answers suggested, you can also add multiple IPs by including other conditions into one directive using the && operator as such:

<If "%{REMOTE_ADDR} != '127.0.0.1' && %{REMOTE_ADDR} != '192.168.1.1'">
    RedirectMatch ^/((?!construction.php).*)$ /construction.php
</If>

See the docs here: http://httpd.apache.org/docs/2.4/mod/core.html#if

Max S.
  • 1,383
  • 14
  • 25
0

Another idea is to give access only to a certain range

RewriteEngine on
RewriteBase /
# Validator
SetEnvIf Remote_Addr "^128.30." IsInt
# Local
SetEnvIf Remote_Addr "^192\.168" IsInt
Order allow,deny
Allow from env=IsInt
speciale
  • 7
  • 1
  • 4
0

If you are interested on having a background image referenced on your construction.php, the code below avoids the image to be redirected:

RewriteCond %{REMOTE_ADDR} !=THE_IP
RewriteCond %{REQUEST_URI} !^\/construction\.php|\/YOUR_IMAGE\.jpg
RewriteRule .* /construction.php [R=302,L]
Mike
  • 21
  • 2
0

Not any one worked until I find my own solution

URL in code: http://www.example.com/index_cons.php

IP address in example is: 75.85.95.105

Tested on lastest version of Cpanel.

RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^75\.85\.95\.105
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "http\:\/\/example\.com\/index_cons\.php" [R=302,L]
JCBiggar
  • 2,477
  • 3
  • 20
  • 33
Kerim Senturk
  • 501
  • 4
  • 4