...

View Full Version : .htaccess redirect everyone but my IP



BlindSeeker
04-07-2009, 11:50 PM
Hey everyone,

I recently moved my site from http://www.tutorialwow.com/ to http://www.tutwow.com/. On the old site, I put this in the htaccess:


Redirect / http://www.tutwow.com/

That redirected everyone (including me) to the new domain. The important thing is that it took everything on the end of the old URL and pasted it onto the new one. For example, if the user went to http://www.tutorialwow.com/contact/, they would get redirected to http://www.tutwow.com/contact/ instead of just http://www.tutwow.com/.

My question is this - is there any way to redirect everyone to the new domain (just like in the code above) except for my IP? Is there some conditional statement that I can use?

Thanks!

Ben

timgolding
04-08-2009, 01:01 AM
i know you can achieve this with php if that helps. Not sure about apache. Here is the php code regardless.



if (!$_SERVER['REMOTE_ADDR']="84.63.49.192") // Replace with your IP
header('Location: http://www.tutwow.com');


That would go at top of page.

Anther solution would be to add http://www.tutorialwow.com as a server alias to http://www.tutwow.com/ which would take /contact/ and everything. Though the URL would still look like the old one but would actually be on the new site. Or setting up both virtual hosts document root to the same location. Either way then you could set up a new subdomain for the old site. Then using htaccess to only allow your IP to access that subdomain.

BlindSeeker
04-08-2009, 01:15 AM
Thanks for the speedy reply!

However, I think I can only use htaccess because I can't put PHP on every page on the site. The thing is, I just need to be able to access the site while other people are being redirected. Isn't there some conditional statement in htaccess?

timgolding
04-08-2009, 02:40 AM
Not that i am aware of. I think setting up a subdomain is the quickest.

BlindSeeker
04-08-2009, 10:47 PM
How would making a subdomain help? The Redirect command redirects everything, including subdomains.

timgolding
04-09-2009, 01:22 AM
Not if you put the redirect rule in the apache conf file for that virtual host.



<VirtualHost 111.22.33.44>
ServerName www.domain.tld
...
Redirect / http://www.new-domain.com/
</VirtualHost>

<VirtualHost 111.22.33.44>
DocumentRoot /www/domain
ServerName www.sub.domain.tld
...
</VirtualHost>

BlindSeeker
04-09-2009, 04:22 AM
Wow, my web host doesn't allow the virtualhost thing. Quite annoying! Any other ways I could do it?

timgolding
04-09-2009, 10:41 AM
There is another way you can use IP blocking with your htaccess that will give a 403 error if the user is not on the IP secified. Then rather than using the standard 403 document you can redirect them to a different one with the following command.



ErrorDocument 403 /newerrordoc.html


Then in that page 'newerrordoc.html' you can put an html redirect command



<meta http-equiv="REFRESH" content="0;url=http://www.the-domain-you-want-to-redirect-to.com">


Oh here's the IP blocking htaccess code



<Limit GET POST>
order deny,allow
deny from all
allow from 111.22.33.44
</Limit>


Another alternative would be looking into using apache mod rewrite. If the above method doesn't work i'll look into that for you.

BlindSeeker
04-09-2009, 05:49 PM
Ok, that worked! Finally found a solution. :)

Thanks for all the help!

schleppel
04-15-2009, 10:08 PM
You can do it with mod_rewrite

Options +FollowSymLinks

RewriteEngine On

# If it's not your IP address
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4$
# Redirect /abc to example.com/abc.
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

BlindSeeker
04-15-2009, 10:30 PM
You can do it with mod_rewrite

Options +FollowSymLinks

RewriteEngine On

# If it's not your IP address
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4$
# Redirect /abc to example.com/abc.
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

Ok, that's more like what I was looking for.

I'm guessing my IP would go where the "1\.2\.3\.4" is, right?

This wouldn't work with subdomains, though, would it?

Thanks!

schleppel
04-15-2009, 11:03 PM
I'm guessing my IP would go where the "1\.2\.3\.4" is, right?
Yes.


This wouldn't work with subdomains, though, would it?
Do you mean abc.example.com to abc.example.net and def.example.com to def.example.net?

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4$
# Match the sub domain (if there is one).
RewriteCond %{HTTP_HOST} ^(.+\.)?example\.com$ [NC]
# %1 is the sub domain (or empty).
RewriteRule ^(.*)$ http://%1example.net/$1 [R=301,L]

BlindSeeker
04-15-2009, 11:46 PM
Yes, that's what I meant.

For some reason, the code isn't working for me. I put in my IP address, keeping the code exactly like you had it, and put it in the htaccess file, but I still get redirected to tutwow.com. Any thoughts?

schleppel
04-15-2009, 11:59 PM
I can't see anything wrong with it.

Have you cleared your browser's cache?

If you use

Options +FollowSymLinks

RewriteEngine On

RewriteRule test http://example.com/?ip=%{REMOTE_ADDR}&host=%{HTTP_HOST} [R,L]
and go to /test do you get the expected value of ip= and host=?

BlindSeeker
04-16-2009, 12:29 AM
Ok, that was my problem. When I did an "ifconfig" on my mac, it must have given me a wrong IP address for some reason. I changed the IP in the code, and it works perfectly now.

Thanks so much for helping!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum