让我们假设我有一个静态HTML网站'example.com/index.html',我有另一个网页'example.com/contact-us.html'。
如果有人想直接访问contact-us.html页面,则会在浏览器中输入网址“example.com/contact-us.html”。但我希望在这种情况下打开索引页面。但是一旦网站加载到带有索引页面的浏览器上,用户就必须能够按照自己的意愿浏览网站上的链接。
这可能吗?如果有,怎么样?
答案 0 :(得分:3)
如果您想阻止用户直接打开除索引之外的页面,您可以检查引用者,如果引用者与您的网站不匹配,则重定向到索引页面。
您使用的是Apache吗?如果是,您可以使用mod_rewrite来执行此操作。
首先,启用mod_rewrite,通常取消注释httpd.conf
文件中的以下行:
LoadModule rewrite_module modules/mod_rewrite.so
然后在与.htaccess
相同的文件夹中创建一个index.html
文件,其中包含以下内容:
RewriteEngine on
RewriteRule ^index.html$ index.html [QSA,L]
RewriteCond %{HTTP_REFERER} !^http://example\.com/.*$
RewriteRule ^.*$ /index.html [R,L]