问题原文
原帖链接 · 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/.htaccessfile: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>
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
