【问题标题】:How to redirect to error-404 page if url in .txt file如果 .txt 文件中有 url,如何重定向到错误 404 页面
【发布时间】:2016-09-21 16:18:24
【问题描述】:

我需要有关 PHP 问题的帮助。

如果网址在 .txt 文件 (block.txt) 中,我如何重定向/阻止我的网站页面

示例 block.txt:

http://www.example.com/search.php?q=query1
http://www.example.com/search.php?q=query2

如果访问者运行block.txt中的url,他将被重定向到error-404页面。

谢谢

【问题讨论】:

标签: php redirect block


【解决方案1】:

一种可能性是使用file_get_contents()in_array() 的组合:

$badURLs = file_get_contents('block.txt');
$badURLArray = explode("\n", $badURLs);

$thisURL = //you define this

if (in_array($thisURL, $badURLArray)) {
    header('Location: http://www.example.com/custom404.html');
}

【讨论】:

  • 很抱歉,这对我不起作用?我不确定这一行:$thisURL = //you define this 问候
【解决方案2】:

不建议将重定向存储在 .txt 文件中

.htaccess 在 PHP 中被引入来处理这些请求

执行以下步骤

1.) 保存一个名为“.htaccess”的文件,不要考虑双引号。

2.) 把这段代码粘贴进去

# we have set here custom error files to handle server errors
ErrorDocument 404 /404.php
ErrorDocument 403 /403.php
ErrorDocument 500 /500.php

您的问题已解决,只需 5 秒即可实施,然后刷新您的网站 :)

【讨论】:

  • 他要求屏蔽一些页面,然后重定向。上面的代码是针对页面未找到的。
【解决方案3】:

对您的 .htaccess 使用这一行解决方案

1 liner mod_alias 基于解决方案:

重定向匹配 403 ^/folder/file.php$

此处的文件夹是您的文件所在的路径,如果不在文件夹中,则使用此代码

1 liner mod_alias 基于解决方案:

重定向匹配 403 ^/file.php$

第二个参数是file.php,它驻留在您要拒绝访问的文件中,它最终会重定向到404 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 2011-07-02
    • 2020-05-16
    • 2023-03-27
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多