Use .htaccess to redirect everyone except you

ah-blabla

New Member
Messages
375
Reaction score
7
Points
0
If you are currently working on a site, say at a subdomain sub.example.com, and would like to have all traffic going there redirected to example.com/page.html, but you still want to be able to access sub.example.com, you can use these htaccess commands:

Code:
# Where you want to redirect to
ErrorDocument 403 http://example.com/page.html
order deny,allow
# You want everyone blocked
deny from all
# Except for your IP (replace with your IP)
allow from 60.118.156.100

This is useful e.g. when you are constructing a site and have already started advertising the address, but don't want it visible to anyone except yourself.

Note: this is a made up IP in the code. Also, dydns domains don't work, since Apache uses reverse lookup to find the domain, so your PTR records must be correct if you're using a domain.
 
Last edited:

ichwar

Community Advocate
Community Support
Messages
1,454
Reaction score
7
Points
0
This is helpful to know.

I do have one question, since we turn of the modem every night to conserve power, each day I have a different IP. Is there any way to work around this so that I don't have to update my .htaccess every day?
 

djalam

Member
Messages
89
Reaction score
2
Points
8
hey ah-blabla, since u seem to be knowledgeable about .htaccess, can you tell me how to make a custom 404 page to redirect to for my html website?
 

playminigames

New Member
Messages
216
Reaction score
6
Points
0
djalam to make a custom 404 page to redirect for your website should be

ErrorDocument 404 YOUR URL HERE

ex: ErrorDocument 404 /error/404.html

Good Luck djalam!
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
If you have a dynamic IP, it would be better off having a user/pass required to view the site except for one page, that basically tells you that the site is in construction.
 

ah-blabla

New Member
Messages
375
Reaction score
7
Points
0
The other option with dynamic IP is to set up dyndns for your router (or for your pc if the router can't), and then redirect in php, e.g you add in the code:
PHP:
<?php
if (($_SERVER['REMOTE_ADDR'])!=gethostbyname("dyndnsaddress.dyndns.org"))
{
header( 'Location: http://newadress.com/bla' ) ;
}
?>
This would have to be right at the beginning of whatever php file is loaded when visitors view the page.
Another option with dyndns is to have a crons script which every 10 - 30 mins checks what the IP address is for your dyndns address, and then modifies the .htaccess file accordingly.

The problem with Apache is it does a reverse lookup, which will fail for a dynamic dns. One possibility is to write an authentication module for apache doing a dyndns check, or to modify the mod_auth module in apache. (I might try this in the future, but I doubt X10hosting would use any old random patch I send in... You'd have to wait until the patch is accepted upstream which could take a while.)
 
Last edited:
Top