htaccess的重写规则 - 检查引荐,如果错了引荐发送到特定的URL,如果正确,允许目录读取错了、重写、发送到、正确

2023-09-02 10:08:18 作者:死在凌晨

我有我的网站上(domain.com/protect)我想限制只有一个引用(otherdomain.com/subfolder)的文件夹。

I have a folder on my site (domain.com/protect) I want to limit to only one referrer (otherdomain.com/subfolder).

拒绝了所有其他人,只允许当从URL来了。

Deny for all others, allow only if coming from that URL.

如果没有从该URL来了,然后重定向访问者交给otherdomain.com/login~~V代替。

If not coming from that URL, then redirect the visitor over to otherdomain.com/login instead.

我怎么会写出来在.htaccess重写规则?

How would I write that out in .htaccess rewrite rules?

推荐答案

在htaccess文件在你的 /保护目录中,添加下列规则:

In the htaccess file in your /protect directory, add these rules:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !otherdomain.com/subfolder
RewriteRule ^ - [L,F]

该条件检查的引用者不包含: otherdomain.com/subfolder ,如果没有,那么无论该请求是(在 /保护目录)将导致403禁止。

The condition checks that the referer doesn't contain: otherdomain.com/subfolder, and if it doesn't, then whatever the request is (inside the /protect directory) will result in a 403 Forbidden.

另外,你可以把这些规则在htaccess的文件在你的文档根目录,如果你宁愿把一切都在一次地方:

Alternatively, you can put these rules in the htaccess file in your document root if you would rather keep everything in once place:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !otherdomain.com/subfolder
RewriteRule ^/?protect/? - [L,F]