.htaccess redirect and IP block

how do I accomplish these two? I just can’t seem to get it right… What I want to do is to just allow myself access to the site and redirect everyone else to the construction page (my site is under construction)

HK,

Please specify what you want to redirect to … or correct this generic code

RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^127\\.0\\.0\\.1$
RewriteRule .? http://www.example.com [L]

This will only allow the localhost (127.0.0.1) to access the directory and all subdirectories but redirect everyone else to example.com - fill in the correct IP address and redirection.

Regards,

DK

Sorry it took me so long to reply (situations in real life came up) anyway, I tried that code but it gave me “This webpage has a redirect loop” so what do you suggest?

Why would you redirect? This seems like a perfect place to use Allow and Deny to me


Order Deny,Allow
Deny from all
Allow from yo.ur.i.p

This will show the website when accessed from your IP, and show a “403 forbidden” page to everyone else

:slight_smile:

but my site is under construction right now, so I was wanting to show the visitors an under construction page.

In that case you might want to have a look at this, where your exact problem is described and solved :slight_smile:

I’m not sure what I’m doing wrong, cause it’s still not working…

What code do you have now?

Well I have a feeling that I have the IP wrong now, but I’m using the IP that’s mentioned at whatsmyip.org, but yet it redirects me?

Yes that IP is the one you’re using to visit your website -unless you have a really strange setup.

What is the code you have now? Feel free to mask out your IP, i.e., w.x.y.z or something.

Well I finally got the redirect working, but now my CSS and images aren’t working, the CSS will work for the maintenance page, but images won’t work on it. This is the code I’m using now:

# MAINTENANCE-PAGE REDIRECT
<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{REMOTE_ADDR} !^this\\.is\\.my\\.ip
 RewriteCond %{REQUEST_URI} !/construction.html$ [NC]
 RewriteCond %{REQUEST_URI} !\\.(jpe?g?|png|gif) [NC]
 RewriteRule .* /construction.html [R=302,L]
</IfModule>

I found this code off of htaccess Redirect to Maintenance Page

If you want css to work too you should modify it as follows


# MAINTENANCE-PAGE REDIRECT
<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{REMOTE_ADDR} !^this\\.is\\.my\\.ip
 RewriteCond %{REQUEST_URI} !/construction.html$ [NC]
 RewriteCond %{REQUEST_URI} !\\.(jpe?g?|png|gif|css)$ [NC]
 RewriteRule .* /construction.html [R=302,L]
</IfModule>

Also, you can get rid of the <IfModule mod_rewrite.c> and </IfModule> once you’ve established mod_rewrite is installed and working, no need to ask that again and again :slight_smile:

Well it finally works now :slight_smile: but now I have another question, it’s concerning the redirect from earlier, i found out why I had such trouble from it, because I was testing it on another computer from within my house, and once I tried it on a proxy site, the redirect worked, I then realized that all of the computers in my house share the same public IP but just different LAN IPs, but I tried using my LAN IP in the .htaccess file, and it redirected me (which is not what I want), so how can I redirect all the computers in the house and everybody else in the world? (though I already accomplished the everybody in the world part lol)

It’s the public IP that you should filter. The internal LAN IP shouldn’t match at all (because that’s hidden behind NAT). Are you saying it does?

Well I’m saying that the other computers in the house have the same public IP according to whatsmyip.org so their not getting redirected like I would want them to.

Ah okay. Yes, you can’t do that. Have you considered installing a web server on your own PC for development purposes? That’s also nicer because it’s all local and thus faster and you don’t have to upload files all the time :slight_smile:

Actually that’s what I’ve been doing for some time now, I just uploaded the files to make sure everything would run smoothly once I got the website running (though I’ve still got a long way to go lol)

Although I’m a day late and a dollar short, the way I do it is to use DirectoryIndex public_link private_link so that the public only see the public link (index.html) while I link directly to the private_link (index.php). That way, I can have visitors see the “under construction” notice while I can see (and provide links for my clients) the “work in progress.”

Regards,

DK