【问题标题】:Catch a misconfigured link in .htaccess or in php在 .htaccess 或 php 中捕获配置错误的链接
【发布时间】:2017-01-05 14:50:20
【问题描述】:

有人像这样链接到我的一个网站: http://example.com/?p=contactus ?p=contactus 不应该在那里。

如何在.htaccess 中捕获?p=contactus 并将其重定向到http://example.com,或者如何在php 中捕获它?

$_REQUEST['p'] 在 php 中返回一个空字符串,我通常使用的 $_REQUEST['page'] 也是如此(我使用漂亮的 URL)。

由于$_REQUEST['page'] 返回一个空字符串,因此链接会退回到我的 index.php;但是,我想避免 Google 认为我有重复的页面。

【问题讨论】:

  • htaccess 中是否已有重写规则?如果您的链接是/?p=contactus,那么$_REQUEST['p'] 不应为空,除非您在Web 服务器中有东西正在重写URL。无论哪种方式,查询字符串参数都不应该自然地影响您所在的页面。
  • 我在 .htaccess 中有这些: RewriteRule ^([^/.]+)$ /index.php?page=$1 [L] RewriteCond $1 !(\.(png|ico|css) |index\.php)$ RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /index.php? [L]

标签: php .htaccess


【解决方案1】:

您可以从 Php 进行重定向。使用 $_SERVER['REQUEST_URI'] 和 $_SERVER['QUERY_STRING'] 获取完整的 url 。如果它有一个查询字符串,那么您可以通过发送 Location http 标头将 url 重定向到您想要的 url。可以使用函数 header("Location: $redirect_url"); 发送;

【讨论】:

  • $_SERVER['QUERY_STRING'] 不起作用,可能是因为我的 .htaccess 文件。 $_SERVER['REQUEST_URI'] 就像一个魅力,它返回 "/?p=contactus" 这使得它很容易捕获并使用 header("Location: example.com") 重定向;谢谢!
【解决方案2】:

试试看是否可行

RewriteEngine on
RewriteCond %{QUERY_STRING} p=contactus
RewriteRule ^/ /?

我觉得我很接近,但重写从来都不是我的强项

【讨论】:

  • 这没有任何区别,我仍然可以在 URL 中看到“?p=contactus”。
  • 对不起,还是一样。
猜你喜欢
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
  • 2017-06-28
  • 2016-03-08
  • 1970-01-01
  • 1970-01-01
  • 2014-10-12
  • 1970-01-01
相关资源
最近更新 更多