Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

.htaccess - Using htaccess block access to a range of URLs except for my IP address

How can I block a range or URL's for all but my IP address.

So ...

Allow http://mydomain.com/* to all users

Block http://mydomain.com/thisdirectoryandallcontents/* to all but my IP

I found some code from another site which might work but I don't have the htaccess skills to adapt it

Options +FollowSymlinks
RewriteEngine on

#urls to exclude
RewriteCond %{REQUEST_URI} !/url-to-exclude-1$
RewriteCond %{REQUEST_URI} !/url-to-exclude-2$
RewriteCond %{REQUEST_URI} !^/images$
RewriteCond %{REQUEST_URI} !^/css$
RewriteCond %{REQUEST_URI} !^/$

#ip to allow access
RewriteCond %{REMOTE_HOST} !^11.11.11.11$

#send to root
RewriteRule .? /http://www.mydomain.com [R=302,L]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have a similar requirement and do basically the same when I put my page (or a part) on maintenance. The content of the htaccess file looks like this:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} /thisdirectoryandallcontents
RewriteCond %{REMOTE_ADDR} !=111.111.111.111
RewriteRule ^.*$ /maintenance.php [R=302,L]

I will explain it step by step according to your requirements:

  1. Every user should be able to access the homepage http://mydomain.com/ - cool, no configuration needed.
  2. The first RewriteCond checks, if the request lies within the folder /thisdirectoryandallcontents.
  3. The second RewriteCond checks, if the IP address is any other then 111.111.111.111 (here you have to write your IP address).
  4. If both of the conditions apply, we redirect the user to /maintenance.php. Here I usually output a message to the user, that the page is currently under maintenance. You could display a message, that the user doesn't have access to /thisdirectoryandallcontents. Instead of redirecting the user to the maintenance page, you could also redirect him to the home page if you wish to.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...