htaccess 限制对所有页面的访问,但引荐来源网址
htaccess restrict access to ALL pages but referrer
我设法使用下面的 .htaccess
指令限制了对我网站的访问。它工作得很好,但我发现除了 referrer 之外,其他人都可以成功访问像 https://example.com/**pages**/
这样的直接页面,然后可以从那里返回主页。我怎样才能限制到除引荐来源网址之外的所有网站(所以所有树都来自我的根 URL)。
# Serve everyone from specific-domain (and internal requests)
RewriteCond %{HTTP_REFERER} ^https?://www\.your-domain\.com/ [OR]
RewriteCond %{HTTP_REFERER} ^https?://www\.specific-domain\.com/
RewriteRule ^ - [L]
# everybody else receives a forbidden
RewriteRule ^ - [F]
根据对您 的讨论,您似乎将这些指令放在了错误的位置。这是一个 WordPress 站点,指令已放置在 之后 WordPress 前端控制器,即。在 # BEGIN WordPress
... # END WordPress
代码块之后。
这实际上是一个很常见的错误。但顺序很重要。
通过将它们放在文件的末尾,它们根本不会被处理到 example.com/<wordpress-url>
的请求,因为请求已经被路由到 WordPress 前端控制器(index.php
).
这些阻塞指令需要放在 .htaccess
文件的 top 处。重要的是,他们必须在 # BEGIN WordPress
部分之前。
您不应将这些指令 放在 WordPress 代码块中,因为 WordPress 维护此部分并且可能会覆盖您放置在此处的任何自定义指令。
您不需要重复 RewriteEngine On
指令(该指令稍后出现在文件中 - 该指令的顺序无关紧要)。事实上,如果有多个 RewriteEngine
指令,那么 last 指令将获胜并控制整个 file/context.
更新#1:
is there a way to exclude a single page from the directives so that this page can still be available even from non referrer - it would be a login page
是的,您可以在第一个块中添加一个附加条件来检查此 URL。例如:
# Serve everyone from specific-domain (and internal requests)
RewriteCond %{REQUEST_URI] ^/login$ [OR]
RewriteCond %{HTTP_REFERER} ^https?://www\.your-domain\.com/ [OR]
RewriteCond %{HTTP_REFERER} ^https?://www\.specific-domain\.com/
RewriteRule ^ - [L]
# Everybody else receives a forbidden
RewriteRule ^ - [F]
更新#2:
但是,由于这是一个 WordPress 站点,您仍然需要继续处理前端控制器(文件后面的 # BEGIN WordPress
部分)以便路由 URL。这可以解释为什么您看到 /<page>
和其他 WordPress URL 的 404,尽管 Referer
可能设置正确。
要解决此问题,请将第一个 RewriteRule
中的 [L]
标志更改为 [S=1]
(跳过 1 条规则),而不是停止进一步处理(L
/ last
标志),它只是跳过以下阻止其他人访问的规则。并继续介绍 WordPress 前端控制器。
例如:
:
RewriteRule ^ - [S=1]
# Everybody else receives a forbidden
RewriteRule ^ - [F]
或者,您可以反转逻辑...
# Block everyone from "other" domains except for specific URLs
RewriteCond %{REQUEST_URI] !^/login$
RewriteCond %{HTTP_REFERER} !^https?://www\.your-domain\.com/
RewriteCond %{HTTP_REFERER} !^https?://www\.specific-domain\.com/
RewriteRule ^ - [F]
# BEGIN WordPress
:
我设法使用下面的 .htaccess
指令限制了对我网站的访问。它工作得很好,但我发现除了 referrer 之外,其他人都可以成功访问像 https://example.com/**pages**/
这样的直接页面,然后可以从那里返回主页。我怎样才能限制到除引荐来源网址之外的所有网站(所以所有树都来自我的根 URL)。
# Serve everyone from specific-domain (and internal requests)
RewriteCond %{HTTP_REFERER} ^https?://www\.your-domain\.com/ [OR]
RewriteCond %{HTTP_REFERER} ^https?://www\.specific-domain\.com/
RewriteRule ^ - [L]
# everybody else receives a forbidden
RewriteRule ^ - [F]
根据对您 # BEGIN WordPress
... # END WordPress
代码块之后。
这实际上是一个很常见的错误。但顺序很重要。
通过将它们放在文件的末尾,它们根本不会被处理到 example.com/<wordpress-url>
的请求,因为请求已经被路由到 WordPress 前端控制器(index.php
).
这些阻塞指令需要放在 .htaccess
文件的 top 处。重要的是,他们必须在 # BEGIN WordPress
部分之前。
您不应将这些指令 放在 WordPress 代码块中,因为 WordPress 维护此部分并且可能会覆盖您放置在此处的任何自定义指令。
您不需要重复 RewriteEngine On
指令(该指令稍后出现在文件中 - 该指令的顺序无关紧要)。事实上,如果有多个 RewriteEngine
指令,那么 last 指令将获胜并控制整个 file/context.
更新#1:
is there a way to exclude a single page from the directives so that this page can still be available even from non referrer - it would be a login page
是的,您可以在第一个块中添加一个附加条件来检查此 URL。例如:
# Serve everyone from specific-domain (and internal requests)
RewriteCond %{REQUEST_URI] ^/login$ [OR]
RewriteCond %{HTTP_REFERER} ^https?://www\.your-domain\.com/ [OR]
RewriteCond %{HTTP_REFERER} ^https?://www\.specific-domain\.com/
RewriteRule ^ - [L]
# Everybody else receives a forbidden
RewriteRule ^ - [F]
更新#2:
但是,由于这是一个 WordPress 站点,您仍然需要继续处理前端控制器(文件后面的 # BEGIN WordPress
部分)以便路由 URL。这可以解释为什么您看到 /<page>
和其他 WordPress URL 的 404,尽管 Referer
可能设置正确。
要解决此问题,请将第一个 RewriteRule
中的 [L]
标志更改为 [S=1]
(跳过 1 条规则),而不是停止进一步处理(L
/ last
标志),它只是跳过以下阻止其他人访问的规则。并继续介绍 WordPress 前端控制器。
例如:
:
RewriteRule ^ - [S=1]
# Everybody else receives a forbidden
RewriteRule ^ - [F]
或者,您可以反转逻辑...
# Block everyone from "other" domains except for specific URLs
RewriteCond %{REQUEST_URI] !^/login$
RewriteCond %{HTTP_REFERER} !^https?://www\.your-domain\.com/
RewriteCond %{HTTP_REFERER} !^https?://www\.specific-domain\.com/
RewriteRule ^ - [F]
# BEGIN WordPress
: