简体   繁体   English

使用 .htaccess 阻止访问子域以及该子域下的所有文件和文件夹

[英]Using htaccess to block access to a subdomain and all files and folders under that subdomain

I have a main domain and a website setup on my cPanel hosting as well as an add-on domain with its own separate website: domain.com and addon.com我在我的 cPanel 主机上有一个主域和一个网站设置,还有一个带有自己独立网站的附加域: domain.comaddon.com

Due to the way cPanel configures add-on domains, the add-on domain's website can be accessed via the main domain:由于 cPanel 配置附加域的方式,可以通过主域访问附加域的网站:

  • addon.domain.com/ addon.domain.com/

  • domain.com/addon/ domain.com/addon/

What I'd like to do is block all access to the add-on domain's website from the main domain, including any and all files or folders contained within, so:我想做的是阻止主域对附加域网站的所有访问,包括其中包含的任何和所有文件或文件夹,因此:

  • Accessing addon.com/anyfolder/anyfile.php works, but;访问addon.com/anyfolder/anyfile.php有效,但是;
  • Accessing addon.domain.com/anyfolder/anyfile.php or访问addon.domain.com/anyfolder/anyfile.php
  • Accessing domain.com/addon/anyfolder/anyfile.php fails with a 404 error.访问domain.com/addon/anyfolder/anyfile.php失败并出现 404 错误。

I have had some success with modifying the add-on domain's.htaccess file to achieve this, for example using this in the add-on domain's.htaccess:我在修改附加域的 .htaccess 文件以实现此目的方面取得了一些成功,例如在附加域的 .htaccess 中使用它:

RewriteEngine on
RedirectMatch 404 ^/addon/(.*)$

Blocks domain.com/addon/anyfolder/anyfile.php perfectly, displaying a 404 error.完美拦截domain.com/addon/anyfolder/anyfile.php ,显示404错误。

However I have tried various methods to do the same for addon.domain.com and although I can usually get it to display a 404 when accessing only the domain, for any folders or files ( addon.domain.com/anyfolder/anyfile.php ) it will still work with no error and I can't figure out how to go about correcting this with.htaccess.但是我已经尝试了各种方法来为addon.domain.com做同样的事情,虽然我通常可以让它在仅访问域时显示 404,对于任何文件夹或文件( addon.domain.com/anyfolder/anyfile.php ) 它仍然可以正常工作,我不知道如何 go 使用 .htaccess 更正此问题。

For example:例如:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^https?://(www\.)addon\.com
RewriteRule ^(.*)$ - [L,R=404]

This makes addon.domain.com display the desired 404 error, but addon.domain.com/anyfolder/anyfile.php still works without issue.这使得addon.domain.com显示所需的 404 错误,但addon.domain.com/anyfolder/anyfile.php仍然可以正常工作。

This is just one example of course, I've actually searched for a while and tried several different rewrite conditions among other things to get the desired result without success.这当然只是一个例子,我实际上已经搜索了一段时间并尝试了几种不同的重写条件,但没有成功获得所需的结果。 Another example that works only for the domain:另一个仅适用于域的示例:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?addon.domain.co.uk$ [NC]
RewriteRule ^(.*)$ - [L,R=404]

Once again, this causes addon.domain.com to display the desired 404 error, but addon.domain.com/anyfolder/anyfile.php still works.这再次导致addon.domain.com显示所需的 404 错误,但addon.domain.com/anyfolder/anyfile.php仍然有效。

The only other thing I can think of doing is editing the main domain's DNS zone and remove (or break somehow) the A record for the subdomain.我唯一能想到的另一件事是编辑主域的 DNS 区域并删除(或以某种方式破坏)子域的 A 记录。 This would of course result in a "server not found" error which would have the desired affect, but I can't imagine it's an advisable option to take.这当然会导致“找不到服务器”错误,这会产生预期的影响,但我无法想象这是一个明智的选择。

You should do something like the following using mod_rewrite near the top of the .htaccess file in the root of the subdomain.您应该使用子域根目录中.htaccess文件顶部附近的 mod_rewrite 执行类似以下操作。

I'm assuming addon.com and www.addon.com should both be accessible.我假设addon.comwww.addon.com都应该可以访问。

For example:例如:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?addon\.com$
RewriteRule ^ - [R=404]

The above will block (serve a 404 Not Found) for any request that is not for addon.com (or www.addon.com ).对于不是针对addon.com (或www.addon.com )的任何请求,以上内容将阻止(提供 404 Not Found)。 The !! prefix on the CondPattern negates the expression. CondPattern上的前缀否定表达式。 Any URL-path.任何 URL 路径。

You do not need the L flag when using a return code in the range 4xx.使用 4xx 范围内的返回码时不需要L标志。


Alternative Method替代方法

An alternative approach that does not use mod_rewrite, so won't necessarily be overridden by .htaccess files in subdirectories:一种不使用 mod_rewrite 的替代方法,因此不一定会被子目录中的.htaccess文件覆盖:

<If "%{HTTP_HOST} !~ /^(www\.)?addon\.com$/">
    Require all denied
</If>

This uses an Apache Expression with mod_authz_core.这使用带有 mod_authz_core 的 Apache 表达式。

The above will serve a 403 Forbidden for any requests that are not for the addon domain.以上将为任何不是针对插件域的请求提供 403 Forbidden。


A look at your rules...看看你的规则...

 RewriteEngine on RedirectMatch 404 ^/addon/(.*)$

RedirectMatch is a mod_alias directive. RedirectMatch是一个 mod_alias 指令。 This is unrelated to the RewriteEngine directive that initialises mod_rewrite.这与初始化 mod_rewrite 的RewriteEngine指令无关。

 RewriteCond %{HTTP_REFERER}?^https:.//(www\.)addon\.com RewriteRule ^(,*)$ - [L,R=404]

This makes addon.domain.com display the desired 404 error, but addon.domain.com/anyfolder/anyfile.php still works without issue.这使得addon.domain.com显示所需的 404 错误,但addon.domain.com/anyfolder/anyfile.php仍然可以正常工作。

This is checking the Referer header (the site you are coming from ), so this will not have the desired result.这是在检查Referer header(您来自的站点),因此不会有预期的结果。 However, a request for addon.domain.com/anyfolder/anyfile.php should still be blocked by this rule.但是,对addon.domain.com/anyfolder/anyfile.php的请求仍应被此规则阻止。

Note that the order of directives in the .htaccess file can be important.请注意, .htaccess文件中指令的顺序可能很重要。 Any blocking directives should be near the top of the config file.任何阻塞指令都应该靠近配置文件的顶部。

Another example that works only for the domain:另一个仅适用于域的示例:

 RewriteEngine on RewriteCond %{HTTP_HOST} ^(www.)?addon.domain.com$ [NC] RewriteRule ^(.*)$ - [L,R=404]

Once again, this causes addon.domain.com to display the desired 404 error, but addon.domain.com/anyfolder/anyfile.php still works.这再次导致addon.domain.com显示所需的 404 错误,但addon.domain.com/anyfolder/anyfile.php仍然有效。

This should block addon.domain.com/anyfolder/anyfile.php (with or without the www subdomain) - unless the rule is put in the wrong place and it is conflicting with other rules, as mentioned above.这应该阻止addon.domain.com/anyfolder/anyfile.php (有或没有 www 子域)- 除非规则放在错误的位置并且与其他规则冲突,如上所述。

Note that you don't necessarily need to have just one rule.请注意,您不一定需要只有一个规则。 Two or more rules can perform the job just as efficiently as one sometimes.两个或多个规则有时可以像一个规则一样有效地执行工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM