Allow access in htaccess based on the HTTP:X-FORWARDED-FOR

谁都会走 提交于 2019-12-24 03:39:11

问题


Hi i should only allow the particular ip address(which is HTTP:X-FORWARDED-FOR adresses) to access the files. I have done it by the following

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP:X-FORWARDED-FOR} !^xxx.xxx.x.xx$
RewriteRule ^$ http://xxx.xxx.x.xx/access_denie.php [R=301,L]

Now i have to allow it for multiple ip for example yyy.yy.y.yy. How can i do it by using htaccess


回答1:


The ^xxx.xxx.x.xx$ portion of your RewriteCond is simply a regular expression. You can easily use groups to add more IP addresses:

^(xxx\.xxx\.x\.xx|yyy\.yy\.y\.yy)$

You will notice I have escaped all the .s with a backslash - this is because . has a special meaning in a regular expression, and it needs to be escaped if you want it to match only a literal . character.

So your new RewriteCond will look like this:

RewriteCond %{HTTP:X-FORWARDED-FOR} !^(xxx\.xxx\.x\.xx|yyy\.yy\.y\.yy)$

You can easily add more IP addresses, simply separate them with | characters.

Please note, however, that this approach does not give you any real security. It would be easy to spoof a request to get past this. If you need security you should use SSL and a proper authentication system instead.



来源:https://stackoverflow.com/questions/13931765/allow-access-in-htaccess-based-on-the-httpx-forwarded-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!