Only allow certain IP addresses to access site with mod_rewrite?

后端 未结 2 1062
小鲜肉
小鲜肉 2020-12-15 12:24

We have a directory on our site which we only want to be accessible by a couple of IP addresses. So we have this .htaccess file to try and get it working:

Re         


        
相关标签:
2条回答
  • 2020-12-15 13:02

    I guess you should add [OR] modifier after each RewriteCond, like this:

    RewriteEngine on
    RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123 [OR]
    RewriteCond %{REMOTE_ADDR} !^124\.124\.124\.124 [OR]
    ...
    
    0 讨论(0)
  • 2020-12-15 13:11

    Forgot to post the answer to this. It was just a typo in the end.. DOH!

    If you notice, some of the %{REMOTE_ADDR} lines have curly brackets, and some have normal brackets! They all needed curly ones.

    RewriteEngine on
    RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123
    RewriteCond %{REMOTE_ADDR} !^124\.124\.124\.124
    RewriteCond %{REMOTE_ADDR} !^125\.125\.125\.125
    RewriteCond %{REMOTE_ADDR} !^126\.126\.126\.126
    RewriteCond %{REMOTE_ADDR} !^127\.127\.127\.127
    RewriteCond %{REMOTE_ADDR} !^128\.128\.128\.128
    
    RewriteCond %{HTTP_REFERER} !^http://www\.example\.com/ [NC]
    RewriteCond %{HTTP_REFERER} !^http://example\.com/ [NC]
    
    RewriteRule ^.*$ http://www.example.com [R=301,L]
    
    0 讨论(0)
提交回复
热议问题