Web Analytics Made Easy -
StatCounter .htaccess redirect everyone but my IP - CodingForum

Announcement

Collapse
No announcement yet.

.htaccess redirect everyone but my IP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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.

    Comment


    • #3
      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?

      Comment


      • #4
        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.

        Comment


        • #5
          How would making a subdomain help? The Redirect command redirects everything, including subdomains.

          Comment


          • #6
            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; Apr 8, 2009, 07:29 PM.
            You can not say you know how to do something, until you can teach it to someone else.

            Comment


            • #7
              Wow, my web host doesn't allow the virtualhost thing. Quite annoying! Any other ways I could do it?

              Comment


              • #8
                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; Apr 9, 2009, 04:45 AM.
                You can not say you know how to do something, until you can teach it to someone else.

                Comment


                • #9
                  Ok, that worked! Finally found a solution.

                  Thanks for all the help!

                  Comment


                  • #10
                    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]

                    Comment


                    • #11
                      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!

                      Comment


                      • #12
                        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]

                        Comment


                        • #13
                          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?

                          Comment


                          • #14
                            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=?

                            Comment


                            • #15
                              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!

                              Comment

                              Working...
                              X