【问题标题】:Block href to specific domains with htaccess使用 htaccess 阻止指向特定域的 href
【发布时间】:2015-09-08 14:34:28
【问题描述】:

我有一个托管在 LAMP 环境中的 php 网站。 许多页面包含对somedomain.com 的引用(例如图像src)。如果 somedomain.com 变得无法访问,我的站点将超时(并出现错误 500)。 有没有办法阻止我的页面的所有 somedomain.com 请求,比如说使用 .htaccess 将它们重定向到 localhost?

【问题讨论】:

  • 我不知道如何处理 .htaccess 文件。但是在php中这很容易。只需请求 somedomain.com 的标头并在 404 上使用 if / else 语句返回。有关更多信息,请参阅this 问题。
  • 这是不可能的,因为客户端(浏览器)在加载这些图像时会调用 somedomain.com。您的 .htaccess 只能为您的服务器管理连接。这个问题有解决方案 - 在您的服务器上使用代理脚本。

标签: php .htaccess url-rewriting url-redirection


【解决方案1】:

不,这是不可能的,因为这些资源是由浏览器加载的,例如图片:

<img src="somedomain.com/img/someimg.png" />

一种可能的方法是使用 PHP 脚本来代理这些资源:

 <img src="myproxy.php?url=http://somedomain.com/img/someimg.png" />

它的样子:

<?php
$content = file_get_contents(@$_GET['url']);

/*
 * There is an error
 */
if (!$content) {
    $content = file_get_contents('alternate_local_img.png');
} 

header('Content-Type: image/png');
echo $content;

可以通过扩展/mime 类型检测来改进。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2014-08-18
    • 1970-01-01
    相关资源
    最近更新 更多