大网数据广告活动
网站运营与故障问答

Deny access to one of two sites using the same .htaccess

· 52CE 我爱测

I need to set up my .htaccess file to deny access to the site if they are accessing the staging.example.com . I have a Magento 2 store and have set up deployment using Capistrano, which will be deploying to 2 separate domains: st…

问题原文

原帖链接 · gabehou · CC BY-SA 3.0

I need to set up my .htaccess file to deny access to the site if they are accessing the staging.example.com.

I have a Magento 2 store and have set up deployment using Capistrano, which will be deploying to 2 separate domains: staging.example.com and example.com. They will have the same .htaccess file and I want to allow access to the staging site by IP only.

Currently using Cpanel and you don't have access to the vhost files.

已采纳解答

原帖链接 · Michael d · CC BY-SA 3.0

You can use something like this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?subdomain\.com$ [NC]
RewriteCond ${ipmap:%{REMOTE_ADDR}} !^(127\.0\.0\.1|192\.168\.|10\.|1\.2\.3\.4)$
RewriteRule ^ - [F]

Source: answer from anubhava on Stack Overflow

You can also allow specific IP addresses and deny the rest with this:

order deny,allow
allow from 31.24.232.14
allow from 127.0.0.0
allow from 192.168.0.0
deny from all

These might also help:

<If "%{HTTP_HOST} != 'www.example.com'">
Redirect / http://www.example.com/
</If>

There's a good chance this last one is what you want. If the code works, it will detect that the website loaded is not example.com (but your subdomain). And if it's your subdomain it will block all IP addresses except the one that you list in the allow from.

<If "%{HTTP_HOST} != 'www.example.com'">
    order deny,allow
    allow from 31.24.232.14
    allow from 127.0.0.0
    allow from 192.168.0.0
    deny from all
</If>

You could also try:

<If "%{HTTP_HOST} = 'subdomain.example.com'">
    order deny,allow
    allow from 31.24.232.14
    allow from 127.0.0.0
    allow from 192.168.0.0
    deny from all
</If>

For Apache 2.4+

If you're going to use require ip, you may need to do it like this:

Require all denied
Require ip 123.123.123.123

Read more: https://stackoverflow.com/questions/37865658/htaccess-allow-deny-ip-using-require-apache-2-4

Maybe this works?:

<If "%{HTTP_HOST} = 'staging.example.com'">
Require all denied
Require ip 123.123.123.123
</if>

Sources are also showing that you need RequireAny to allow only specific IPs. If that's the case you may have to modify it for your subdomain requirement condition:

<RequireAny>
Require ip 1.2.3.4
Require ip 23.34.45.56
</RequireAny>

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2018-03-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。