I have done several attempts to figure out how to redirect visitors from the visitors hostname to some other place.

I know with IP this works:

RewriteCond %{REMOTE_ADDR} ^185\.93\.180\.* [OR]
RewriteCond %{REMOTE_ADDR} ^188\.143\.232\.31
RewriteRule ^(.*)$ attacker_reroute.php

But in this case, I need to do it by domain, any suggestions?

Something like

RewriteCond %{REMOTE_HOST} *\.badguys\.com
  • Do you have access to the server config?
    – MrWhite
    Apr 7 at 0:35

The REMOTE_HOST server variable holds the resolved hostname of the remote IP address accessing the site. However, whether this contains the resolved hostname or not is dependent on HostnameLookups On being set in the server config. It is Off by default and many hosts disable this feature for performance reasons. This cannot be enabled in .htaccess.

If the HostnameLookups is not enabled then REMOTE_HOST simply contains the same as REMOTE_ADDR, ie. the IP address of the machine making the request.


Be careful of your regex, you seem to be treating * like a "wildcard" character. In regex * is a quantifier meaning "0 or more of the preceding item".


This is matching "185.93.180." followed by any number of dots. It should be ^185\.93\.180 or ^185\.93\.180\. - just remove the *.


This is completely invalid. If you want to match any subdomain of "badguys.com" then you need the regex \.badguys\.com$ (basically remove the *). To match the domain apex or any subdomain then (^|\.)badguys\.com$.

  • Hello, Thanks for the info. Unfortunately its't taking this like an overall wildcard. I have This: RewriteCond %{REMOTE_HOST} ^|\.badguys\.com$<br> RewriteCond %{REMOTE_HOST} ^|\.ip-51-255-209\.eu$<br> RewriteRule ^(.*)$ GoSomwhere.com<br> Apr 7 at 2:57
  • @user9604446 Please edit your question to add your code. Comments are not for posting blocks of code - it's impossible to read as specific characters can get "lost" in the unformatted text. In the code you posted, it looks like the parentheses are missing? What do you mean by "its't taking this like an overall wildcard"? (If the parentheses are really missing then yes, it will always redirect, if that's what you mean by "overall wildcard"?) Have you confirmed that REMOTE_HOST does contain the resolved hostname?
    – MrWhite
    Apr 7 at 22:52

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.