【问题标题】:Allow only access to a specific mod_rewrite generated URL, deny all other http requests只允许访问特定的 mod_rewrite 生成的 URL,拒绝所有其他 http 请求
【发布时间】:2014-02-24 06:12:14
【问题描述】:

我有一个由 www.mysite.com 上的 apache2 提供的 REST API。

其余的 api 可在 www.mysite.com/rest/...

但是盒子上没有名为/rest的文件夹,都是重写规则发生的。

如何使用 .htaccess 文件实现以下功能:

1) 允许访问来自 www.mysite.com/rest/... 的所有请求(即任何请求,只要它是虚拟文件夹 /rest 的子目录)

2) 仅允许来自特定 IP 地址的任何其他请求,例如虚构 IP 123.45.67.89;对于所有其他 IP 地址,拒绝

原因是我不希望任何人登陆 www.mysite.com 的管理页面。

谢谢

httpd.conf的内容如下:

<Directory "/opt/bitnami/apps/myapp/htdocs/web">
Options -Indexes +FollowSymLinks -MultiViews
AllowOverride None

<IfVersion < 2.3 >
 Order allow,deny
 Allow from All
</IfVersion>
<IfVersion >= 2.3>
 Require all granted
</IfVersion>
                   <IfModule pagespeed_module>
                   ModPagespeedDisallow "*"
                   </IfModule>
RewriteEngine on
RewriteBase /

#   Force login to be SSL
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(.+)\.cloud\.myapp\.com$ [NC]
RewriteRule ^/?web/login/?(.*) https://%{SERVER_NAME}/web/login/$1 [R,L]

# if ack directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule ^.*$ /index.php [L]
### END: .htaccess inline ###

<LimitExcept GET HEAD PUT DELETE PATCH POST>
    Allow from all
</LimitExcept>

<IfDefine USE_PHP_FPM>
   RewriteEngine On
   RewriteOptions Inherit

   RewriteRule ^(.*\.php(/.*)?)$ fcgi://uds=%2fopt%2fbitnami%2fphp%2fvar%2frun%2fmyapp.sock/%{REQUEST_FILENAME} [P,L]
</IfDefine>

包括“/opt/bitnami/apps/myapp/conf/htaccess.conf”

【问题讨论】:

  • 发布您当前的 htaccess 规则。
  • 我已经这样做了作为原始帖子的编辑。
  • @PanamaJack:我现在添加了一些指令,这些指令可以正确检测 IP 是否不是 123.45.67.89,但它错误地拒绝访问所有内容,包括 /rest/ 下的任何内容...我有 RewriteCond % {REQUEST_FILENAME} !rest/ ....then.... RewriteRule ^(.+) /tmp/$1 [L] 我需要什么小调整?

标签: .htaccess mod-rewrite apache2


【解决方案1】:

尝试替换这个

<IfVersion < 2.3 >
 Order allow,deny
 Allow from All
</IfVersion>
<IfVersion >= 2.3>
 Require all granted
</IfVersion>

在下面看看它是如何工作的。

Order deny,allow
Deny from all
Allow from 123.456.67.89

123.456.67.89 IP 地址替换为您的 IP 地址

然后在 /rest 文件夹的另一个目录下添加另一个目录指令。

<Directory /opt/bitnami/apps/myapp/htdocs/web/rest>
Order Allow,Deny
Allow from All
</Directory>

它是如何工作的。同样,您也可以在 root 和 /rest 的 htaccess 中执行此操作。

【讨论】:

  • 这具有向所有人提供 403 禁止消息的效果,包括指定的 IP 地址。所有请求都会发生这种情况,包括 /rest。请记住,../rest 没有物理文件夹,那么目录指令真的可以在 .conf 中工作吗?另外,如果这是原因,我如何将这些转换为 htaccess 重写指令。
猜你喜欢
  • 2013-09-21
  • 2012-09-18
  • 2014-07-15
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多