HOW TO:使用htaccess将网址限制为特定IP [英] HOW TO: Restrict url to specific IP using htaccess

查看:69
本文介绍了HOW TO:使用htaccess将网址限制为特定IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要屏蔽特定的网址,例如www.domain.com/admin/login/?使用htaccess并授予仅访问特定IP地址的url的权限.

I need to block a specific URL that looks like www.domain.com/admin/login/? using the htaccess and grant permission to access the url only for specific IP addresses.

我们已经尝试过了,并且部分有效.这 "?" URL中似乎有问题.当我包括?在htaccess中以及如下所示的URL中,它将无法正常工作.我在这里做错什么了?

We have already tried this and it partially works. The "?" in the URL seems to be giving problem. When I include the ? in the htaccess along with the URL as shown below, it won't work the way it is required to work. What am i doing wrong here?

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !=xxx\.xxx\.xxx\.xxx
RewriteRule admin/login/?$ / [R=301,L]

现有htaccess

更新: 我有一个自定义CMS,它没有典型的树结构,即http://www.mysite.com/folder/var/www/folder不对应,实际上http://www.mysite.com/folder根本没有单个文件或目录表示形式.因此,对于/folder没有相应的.htaccess文件.我看不到如何将URL而不是目录附加到重写规则.这是我一直在玩的重写概念:

UPDATE: I’ve a custom CMS and it doesn't have a typical tree stucture, i.e. http://www.mysite.com/folder doesn't correspond to /var/www/folder, and in fact http://www.mysite.com/folder doesn't have a single file or directory representation at all. Thus there is no corresponding .htaccess file for /folder. I don't see how to attach a URL instead of a directory to the rewrite rule. Here are the rewrite concepts I have been playing with:

这项工作有效吗?我在网上找到了这个

Will this work? I found this online:

<Location /folder>
Order deny,allow
deny from all
allow from xxx.xxx.xxx.xxx
allow from xxx.xxx.xxx.xxx
</Location>

UPDATE#2: 问题已解决.问题出在我使用Cloudflare的原因上.这使所有请求都从cloudflare发送到了url.在服务器上安装mod_cloudflare并使用以下脚本后,我成功地阻止了仅允许指定IP的公共访问.

UPDATE#2: The problem has been solved. The problem was due to the reason that I was using Cloudflare. This made all the request to the url from cloudflare. Upon installing mod_cloudflare on the server and using the below script, I was successfully able to block access to public allowing only specified IP's.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)admin(.*)$
RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx$
RewriteRule .* / [R=302,L]
</IfModule>

推荐答案

此规则应可用于阻止该URL:

This rule should work for blocking of that URL:

RewriteEngine on

RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx$
RewriteRule ^admin/login/? - [F,NC]

更新:将此代码放在/admin/.htaccess中:

RewriteEngine on

RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx$
RewriteRule ^login/? - [F,NC]

这篇关于HOW TO:使用htaccess将网址限制为特定IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆