Search code examples
.htaccesshttp-redirecthttp-status-code-301

301 Redirect on Error to Different Domain


Currently I'm blocking certain users IP addresses using

<Limit GET POST>
 order allow,deny
 allow from all  
 deny from 3.0.0.0/8
 deny from 4.0.0.0/8
 deny from 5.152.184.0/21
 deny from 6.0.0.0/7
 deny from 8.0.0.0/7
 deny from 11.0.0.0/8
</Limit>

Which then once the 403 error is triggered, is redirecting them to a new domain using

ErrorDocument 403 http://www.domain.com

My question is instead of 403 redirecting them to the external domain, simply do a 301 redirect for the given url to the new domain.

Basicall, how can I trigger this code

RewriteCond %{HTTP_HOST} \olddomain.com$
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

For the IP addresses which I am denying?


Solution

  • You can get rid of your <Limit> container and use mod_rewrite like this:

    RewriteEngine On
    
    RewriteCond %{REQUEST_METHOD} ^(GET|POST)$
    RewriteCond %{REMOTE_ADDR} ^((3|4|6|8|11)\.|5\.152\.(184\.185\.186\.187\.188\.189\.190\.191))
    RewriteCond %{HTTP_HOST} olddomain\.com$
    RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]