.htaccess 确定引用者是否不是当前域

发布于 2025-01-06 01:19:09 字数 769 浏览 4 评论 0原文

我正在尝试创建一个 .htaccess mod_rewrite,如果当前引用者不是我自己的域,它的行为会有所不同。例如,假设我拥有 example.com(即 www.example.com、http://example.com 等) 。当有人访问 example.com(或 beta.example.com 等子域)时,我想忽略此 .htaccess 规则。所以我猜正则表达式基本上只会在其中查找 example.com 并忽略它们。

但是,如果像 otherdomain.com 这样的域(假设通过 cname 或 A 记录指向 example.com)访问我的网站,我想将它们重定向到某个地方。到目前为止,我认为已经接近但不起作用。

我对这些规则的主要困惑是 RewriteRule 之后的部分(在本例中为 ^$)。我在谷歌搜索中看到了一些不同的东西,但我不确定其中的差异。例如,我也只看到了 ^、(.*)$ 等。

RewriteCond %{HTTP_REFERER} !^http://([^.]+\.)*(example)\.com
RewriteRule ^$ redirectfile.php [L]

我也一直在搞乱

RewriteCond %{HTTP_REFERER} ^http://(.+\.)*\.com
RewriteCond %1 !^(example)\.$
RewriteRule ^$ redirectfile.php [R=302,L]

I'm trying to create an .htaccess mod_rewrite that will behave differently if the current referer isn't my own domain. For instance, say I own example.com (i.e. www.example.com, http://example.com, etc). When somebody goes to example.com (or an subdomain such as beta.example.com), I want to ignore this .htaccess rule. So I guess the regex would basically just look for example.com somewhere in it and ignore those.

However, is a domain such as otherdomain.com (which is assumed to point to example.com via cname or A-record) access my site, I want to redirect them somewhere. Here's what I have so far that I believe is close but isn't working.

My main confusion with these rules is the part that comes after the RewriteRule (^$ in this case). I've seen a few different things put there in my Googling and I'm not sure on the differences. For instance, I've also seen just a ^, a (.*)$, etc.

RewriteCond %{HTTP_REFERER} !^http://([^.]+\.)*(example)\.com
RewriteRule ^$ redirectfile.php [L]

I've also been messing with

RewriteCond %{HTTP_REFERER} ^http://(.+\.)*\.com
RewriteCond %1 !^(example)\.$
RewriteRule ^$ redirectfile.php [R=302,L]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吝吻 2025-01-13 01:19:09

尝试一下,

RewriteCond %{HTTP_REFERER} !^http://([^.]+\.)*example\.com 
RewriteRule ^ redirectfile.php [L]

您有 ^$,它仅与主页匹配。

我将其更改为 ^ ,它将匹配每个请求,假设您想要匹配每个请求。

(.*)$ 也会匹配每个请求,但也会捕获匹配,以便您在目标中重用为 $1

Try

RewriteCond %{HTTP_REFERER} !^http://([^.]+\.)*example\.com 
RewriteRule ^ redirectfile.php [L]

You had ^$, which would only match the home page.

I changed it to just ^ which will match every request, assuming that you want to match every request.

A (.*)$ would also match every request, but also capture the match so that you reuse in in the target as $1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文