Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Location
    United States
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    .htaccess redirect everyone but my IP

    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:

    Code:
    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

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    i know you can achieve this with php if that helps. Not sure about apache. Here is the php code regardless.

    PHP Code:
    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.
    You can not say you know how to do something, until you can teach it to someone else.

  • #3
    New to the CF scene
    Join Date
    Apr 2009
    Location
    United States
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Not that i am aware of. I think setting up a subdomain is the quickest.
    You can not say you know how to do something, until you can teach it to someone else.

  • #5
    New to the CF scene
    Join Date
    Apr 2009
    Location
    United States
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How would making a subdomain help? The Redirect command redirects everything, including subdomains.

  • #6
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Not if you put the redirect rule in the apache conf file for that virtual host.

    Code:
     <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>
    Last edited by timgolding; 04-09-2009 at 12:29 AM.
    You can not say you know how to do something, until you can teach it to someone else.

  • #7
    New to the CF scene
    Join Date
    Apr 2009
    Location
    United States
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow, my web host doesn't allow the virtualhost thing. Quite annoying! Any other ways I could do it?

  • #8
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    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.

    Code:
    ErrorDocument 403 /newerrordoc.html
    Then in that page 'newerrordoc.html' you can put an html redirect command

    Code:
    <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

    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.
    Last edited by timgolding; 04-09-2009 at 09:45 AM.
    You can not say you know how to do something, until you can teach it to someone else.

  • #9
    New to the CF scene
    Join Date
    Apr 2009
    Location
    United States
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, that worked! Finally found a solution.

    Thanks for all the help!

  • #10
    Regular Coder
    Join Date
    Oct 2004
    Posts
    363
    Thanks
    0
    Thanked 18 Times in 18 Posts
    You can do it with mod_rewrite
    Code:
    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]

  • #11
    New to the CF scene
    Join Date
    Apr 2009
    Location
    United States
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by schleppel View Post
    You can do it with mod_rewrite
    Code:
    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!

  • #12
    Regular Coder
    Join Date
    Oct 2004
    Posts
    363
    Thanks
    0
    Thanked 18 Times in 18 Posts
    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?
    Code:
    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]

  • #13
    New to the CF scene
    Join Date
    Apr 2009
    Location
    United States
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #14
    Regular Coder
    Join Date
    Oct 2004
    Posts
    363
    Thanks
    0
    Thanked 18 Times in 18 Posts
    I can't see anything wrong with it.

    Have you cleared your browser's cache?

    If you use
    Code:
    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=?

  • #15
    New to the CF scene
    Join Date
    Apr 2009
    Location
    United States
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •