【问题标题】:Prevent direct traffic to URL using htaccess使用 htaccess 防止直接访问 URL
【发布时间】:2015-06-29 15:31:31
【问题描述】:

我需要防止直接访问 URL (http://www.example.com/gated-asset)。有没有办法向 htaccess 文件添加代码,将所有直接流量重定向到另一个页面 (http://www.example.com/form)?

我在我的 htaccess 文件中尝试了以下代码,但所有页面,包括主页,都重定向到 www.example.com/form。

RewriteEngine On 
RewriteCond %{HTTP_REFERER} !^http://go.example.com [NC] 
RewriteRule .* http://www.example.com/form [R,L]

整个 .htaccess 文件如下所示(它是一个 WordPress 网站):

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

RewriteEngine On 
RewriteCond %{HTTP_REFERER} !^http://go.example.com [NC] 
RewriteRule .* http://www.example.com/form [R,L]

我还尝试了以下方法:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://go.example.com [NC]
RewriteRule ^/$ /form/ [R,L]
RewriteRule ^/$ /gated-asset/ [R,L]

还有:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://go.example.com [NC]
RewriteRule http://www.example.com/gated-asset http://www.example.com/form/ [R,L]

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    如果您匹配开头 (^),您需要在推荐人检查中包含 http://。与HTTP_HOST 不同,HTTP_REFERER 需要它。此外,引用 URL 可能包含REQUEST_URI,因此在域中关闭表达式将使其无法正常工作。

    RewriteEngine On 
    RewriteCond %{HTTP_REFERER} !^http://go.example.com [NC] 
    RewriteRule .* http://www.example.com/form [R,L]
    

    如果您不想包含http://,则可以改用以下条件:

    RewriteCond %{HTTP_REFERER} !go.example.com [NC]
    

    但是,我建议使用第一个示例。

    这里还包括RL 标志。 R 如果重写是针对外部资源,则隐含,但通常最好包含它。

    【讨论】:

    • 感谢您的澄清。不幸的是,主页仍在被重定向。我只需要重定向一个特定的页面。你能提供一个解决方案吗?
    • 您的.htaccess 文件或应用程序/脚本中可能有另一个指令重定向到主页。您可能应该使用调试工具来查看可能导致重定向的原因。 Chrome 控制台的 Network 选项卡是一个不错的起点。
    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    相关资源
    最近更新 更多