Redirect certain IP's to another page on the same site?

Assuming that you wanted people on certain IP address’s to go to another page on your own website (example.com/websitepage) - would this be correct please?

RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^56\\.150\\.186\\.229$ [OR]
RewriteCond %{REMOTE_ADDR} ^89\\.103\\.221\\.49$ [OR]
RewriteCond %{REMOTE_ADDR} ^25\\.53\\.91\\.24$ [OR]
RewriteCond %{REMOTE_ADDR} ^82\\.151\\.156\\.151$
RewriteRule ^(.*)$ http://www.example.com/websitepage [L]

Also, what do the dollar signs do please? Don’y quite understand that bit?

Yep that will work.

The $ signs indicate “end of string”, whereas the ^ signs indicate “start of string”.

So if we take the third line for example, that has

^25\\.53\\.91\\.24$

That will only match one IP: 25.53.91.24, and nothing else.

Now, if you take out the ^ at the start, it will also match 125.53.91.24, 225.53.91.24, 325.53.91.24, 12.254.21.20.25.53.91.24
(Yes, I realize the last two examples aren’t valid IPs)

Analogously, if you remove the $ at the end, the expression could also match 25.53.91.241, 25.53.91.242, 25.53.91.243, 25.53.91.24.21.124.124.121, etc.

Does that make sense?

I had to read and reread it, but yes, I see it now and thank you for that explanation. :slight_smile:

One thing, supposing you wanted to get all IP’s within a certain range, ie,
56.150.186.
How would you do that please?

Lastly, I’ve seen that the code just below does work without the \'s, so why do we need them, is it less of a hit on resources if we use them?

RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^56.150.186.229$ [OR]
RewriteCond %{REMOTE_ADDR} ^89.103.221.49$ [OR]
RewriteCond %{REMOTE_ADDR} ^25.53.91.24$ [OR]
RewriteCond %{REMOTE_ADDR} ^82.151.156.151$
RewriteRule ^(.*)$ http://www.example.com/websitepage [L]

Aw, with the info in the post above you could have answered that yourself. It’s in all the examples of leaving out the $

No matter.


RewriteCond %{REMOTE_ADDR} ^56\\.150\\.186\\. [OR]

So, just leave out the $ at the end :slight_smile:

Indeed leaving out the backslash also works, but you’ve then changed the meaning from “match a dot” to “match any chacter”, since that’s what a dot does in regex. So, 56.150.186.229 will also match 56a150b186c229 for example, whereas 56\.150\.186\.229 will not.

And even though nobody has an (IPv4) IP with letters in them, it’s still faster to use the backslashes because the regular expression engine will go look for a dot specifically, instead of “anything”.

Thanks, that’s answered it completely. I obviously did wonder about the dollar sign, as I wasn’t sure if removing it, made that particular line run into the next line. :slight_smile:

Nah, in .htaccess a line (rule, condition, whatever) ends at the carriage return. Plain and simple :slight_smile:

Interesting, so if the line always ends, at the end :slight_smile: why put a $ sign at the end?

Also, can you see any reason why :

RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^56\\.150\\.186\\.229$ [OR]
RewriteCond %{REMOTE_ADDR} ^89\\.103\\.221\\.49$ [OR]
RewriteCond %{REMOTE_ADDR} ^25\\.53\\.91\\.24$ [OR]
RewriteCond %{REMOTE_ADDR} ^82\\.151\\.156\\.151$
RewriteRule ^(.*)$ http://www.example.com/websitepage [L]

wouldn’t send people with those IP address’s to :

http://www.example.com/websitepage

??

Because you’re not using it as the end of the line, but to indicate you want to match something at the end of the subject string. Two completely different things.


RewriteCond %{REMOTE_ADDR} ^25\\.53\\.91\\.24$ [OR]
                                          ^ Match at end of string
                                                ^ End of line

RewriteCond %{REMOTE_ADDR} ^25\\.53\\.91\\.24 [OR]
                                               ^ End of line

When I remove the $ the “end of line” is still there, I’ve only removed “Match at end of string”.

Does that make sense?

No, it works fine for me. Though I would get rid of the :redhot: (.*) :redhot:

RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^56\\.150\\.186\\.229$ [OR]
RewriteCond %{REMOTE_ADDR} ^89\\.103\\.221\\.49$ [OR]
RewriteCond %{REMOTE_ADDR} ^25\\.53\\.91\\.24$ [OR]
RewriteCond %{REMOTE_ADDR} ^82\\.151\\.156\\.151$
RewriteRule .? http://www.example.com/websitepage [L]

Also, if you want this to be a permanent redirect, change [L] to [L,R=301].
Currently it is a temporary redirect.

How are you using the $ sign to match something ? It used as a wild card then ?

So, for a permanent redirect, should the complete doings be :

RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^56\\.150\\.186\\.229$ [OR]
RewriteCond %{REMOTE_ADDR} ^89\\.103\\.221\\.49$ [OR]
RewriteCond %{REMOTE_ADDR} ^25\\.53\\.91\\.24$ [OR]
RewriteCond %{REMOTE_ADDR} ^82\\.151\\.156\\.151$
RewriteRule http://www.example.com/websitepage [L,R=301]

It’s not “matching something”. It’s indicating how/where the regular expression engine should match. I’m afraid I can’t make it any more clear than I tried in the second post of this thread. Maybe Regex Tutorial - Start and End of String or Line Anchors will help?

Yes :slight_smile:

Thanks - I’m getting there :slight_smile:

Lastly, what is the last line below supposed to do please ? Is it coded ok ?


RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^Zeus
RewriteRule ^.* - [F,L]

Yes, it’s coded properly, and it says: if (as per the RewriteCond) the User-Agent starts with (as per ^) “Zeus”, don’t redirect anywhere (as per -) and deny access to any (as per ^.* – which I would replace with .? by the way) request (as per [F])

Sorry, didn’t understand the bit to replace? Also, what would someone see from that ua? They wouldn’t get a 403 page? Do they get some sort of message?

A RewriteRule normally redirects from one point to another


RewriteRule from to [flags]

Or, it’s a rule that rewrites URLs, hence the name :slight_smile:

However in your rule, to is empty, which means don’t rewrite this all.
You don’t need to rewrite it, because you just want to show a “forbidden” page, not another URL.

As for what the user sees, what happened when you tried? :wink:

Why would I see what that ua would see?

Because you can change the RewriteCond to look for the UA of your browser instead of Zeus, and the visit the page Zeus isn’t supposed to see :slight_smile:

You can see your User Agent on What is My IP Address, ISP, Host, User Agent and Proxy IP @ Spyber.com