【问题标题】:How to forward the HTTP_REFERER to a page from htaccess?如何将 HTTP_REFERER 从 htaccess 转发到页面?
【发布时间】:2020-07-18 16:43:54
【问题描述】:

当有人点击我的站点根文件夹 (www.myDomain.com) 并且 Index.php 页面在此之后得到它,我如何将 HTTP_REFERER 转发到 Index.php 页面以便我可以记录它?

我有这个但没有快乐。

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^mydomain.com [NC]
RewriteRule ^/?index.php?%{HTTP_REFERER}$ mydomain.com/ [L,R]

谢谢

【问题讨论】:

  • 请进一步解释 - 为什么需要这样做?
  • 我网站的链接在多个网站上。如果他们点击的链接是 www.mysite.com/index.php,我会得到推荐人。如果用户单击指向根目录 (www.mysite.com/) 的链接,那么当我尝试将其收集到 Index.php 文件中时,我就会丢失引荐来源。

标签: php html apache .htaccess


【解决方案1】:

确保启用了 apache mod_rewrite。然后重定向:

<IfModule mod_rewrite.c>
    # Make sure url's can be rewritten.
    RewriteEngine on

    # Redirect requests that are not files or directories to index.php.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]
</IfModule>

这是大多数 CMS 的做法。这确实意味着如果他们将直接路径写入文件,他们将看到该文件。所以你会想要这之前加上类似的东西:

# Prevent direct access to these file types.
<FilesMatch "\.(sql|md|sh|scss|rb|conf|rc|tpl(\.php)?|lib(\.php)?|wig(\.php)?|pg(\.php)?|reg(\.php)?|db(\.php)?)$">
  Require all denied
</FilesMatch>

这意味着他们无法访问上面列出的文件扩展名。您可以只用rc|php 替换最后几个文件,但我允许访问未使用 .lib 进行的 .php 文件,因此我可以执行受浏览器身份验证保护的脚本,以进行调试和银弹数据库修复。

您的 apache 配置中还必须有类似的内容,允许在您的站点目录中读取 .htaccess。

<Directory "C:/Sites">
    AllowOverride All
</Directory>

【讨论】:

  • 太棒了。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-09
  • 2010-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
相关资源
最近更新 更多