I've got a Apache web server that serves 2 domains, now in my school one domain is blocked; one isn't (same webpage for the moment). I want that if people connect to my old domain (the one that isn't blocked) to get redirected, unless it comes from the school's IP address. How would I do this, I know it has something to do with .htaccess but I don't know how to do this.


Try something like the following near the top of your .htaccess file (using mod_rewrite):

RewriteEngine On
RewriteCond %{HTTP_HOST} olddomain\.com [NC]
RewriteCond %{REMOTE_ADDR} !203\.0\.113\.123
RewriteRule (.*) http://newdomain.com/$1 [R=301,L]

Where is the school's external IP address.

This is the same as any other domain to domain redirect, just with an additional condition to exclude requests coming from the above IP address.

UPDATE: If you do this redirect in your main server config, then you may need to change the above RewriteRule slightly. If it's in a <Directory> container then leave as-is, otherwise if it's directly in the server config (or virtual host) then change it to something like:

RewriteRule ^ http://newdomain.com%{REQUEST_URI} [R=301,L]
  • I've set this up but I need to test it at home to make sure it works from another IP. – Keaton Lair - VE5LPL May 29 '17 at 20:03
  • 1
    Then change the school IP address to your home IP address... you are expected to think a little :) – Simon Hayter May 29 '17 at 20:47
  • ...but make sure the browser cache is cleared afterwards (or test with a 302). – DocRoot May 29 '17 at 20:55
  • Nope...Didn't work... I added in my .htaccess "ErrorDocument 404 /404.html" and that didn't work either... – Keaton Lair - VE5LPL May 30 '17 at 16:59
  • 1
    ".htaccess does not appear to respond to to the directives I give it" - Ah... If with that ErrorDocument directive, the file /404.html is not served and you just get the standard 404 response then it sounds like .htaccess files are not enabled. You'll need something like AllowOverride All in the appropriate <Directory> container in the server config. If you then start getting 500 errors then you'll also need to install/enable mod_rewrite. However, if you have access to the server config, it will be preferable to do this in your server config instead of .htaccess. – DocRoot May 31 '17 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.