简体   繁体   中英

How to redirect to error-404 page if url in .txt file

I need help with an issue in PHP.

How I can redirect/block a page of my website if the url is in a .txt file (block.txt)

Example block.txt:

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

If a visitor run the url in the block.txt, he will be redirected to error-404 page.

Thank you

One possibility is to use a combination of file_get_contents() and 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');
}

It is not recommended to store redirects in .txt files

.htaccess has been introduced in PHP to handle these request

Do the following steps

1.) Save a file with name ".htaccess", do not consider the double quotes.

2.) Paste this code in it

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

And your problem is solved just will take 5 secs to implement it then just refresh your site :)

Use this one line solution to your .htaccess

1 liner mod_alias based solution :

RedirectMatch 403 ^/folder/file.php$

here folder is the path in which your file exists if not in folder then use this code

1 liner mod_alias based solution :

RedirectMatch 403 ^/file.php$

the second parameter is file.php which resided to the file which you want to deny access it will redirect to 404 eventually :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM