大网数据广告活动
网站运营与故障问答

How to block access to all but homepage unless that access comes from the homepage?

· 52CE 我爱测

Given a homepage at domain.com (e.g., https://example.com/ ), I want: outside requests to only be able to visit the homepage and no subpages that homepage to successfully redirect visitors to a subpage (for example, https://examp…

问题原文

原帖链接 · mix · CC BY-SA 4.0

Given a homepage at domain.com (e.g., https://example.com/), I want:

  1. outside requests to only be able to visit the homepage and no subpages
  2. that homepage to successfully redirect visitors to a subpage (for example, https://example.com/page1.html)

I can accomplish #2 with some simple JavaScript and window.location.href = example.com/page1.html. But for #1, is there a way to reject access to internal pages if the referrer is coming from my domain.com? Basically the flow I'm looking for is:

  1. outside visitor visits https://example.com/
  2. https://example.com/ redirects visitor to, for example, example.com/page1.html
  3. any attempt to visit example.com/page1.html directly fails with a 404

I get that this may all seem nonsensical in terms of what it does. That's by design. I want a website that sends visitors to internal pages, but only if they get there through the homepage redirecting them to it.

已采纳解答

原帖链接 · Itai · CC BY-SA 4.0

There is a simple way if you want the rules to be followed but not enforce them against deliberate spoofing. This looks something like this:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://www.example.com/.*$
RewriteRule (.+) - [R=404,L]

Someone with intent on bypassing this can spoof the referrer though. A better way would be for you to enforce visiting the home-page first in your code. Basically, you read a cookie and if it is not there, redirect to the home page. If it is there, do not redirect. Make the home page be the only one that sets the cookie, and they have to visit the home page first.

In PHP the function called session-start can be used for this since it starts or resumes a session. You did not say what back-end you are using but there are probably equivalents in other languages.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2021-03-09)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。