问题原文
原帖链接 · mix · CC BY-SA 4.0
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://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:
- outside visitor visits
https://example.com/ https://example.com/redirects visitor to, for example,example.com/page1.html- any attempt to visit
example.com/page1.htmldirectly 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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
