Use PHP to rewrite the 404 error page to make your page more friendly _php tips

Source: Internet
Author: User
Tags rtrim
404 error, many people know that if you want to access the URL does not exist when you read the display of this page. In the past in dealing with 404 aspects of our usual practice is to write a few lines of words, and the interested people may be a little landscaping, and a few other want to opportunistic guys even use meta tag to do the delay turn, In fact, 404 can also help us do a lot of things, this is the focus of our discussion today.

first of all, let me explain what my 404 can do for me .:
1. He can tell me when the user from where to visit me which URL results failed.
2. He can tell me why they want to access this URL and help me optimize the site
3. He can tell me the IP address of each other.
4. He can tell me what the man's intentions are.
5. I can get him to shut up about what he doesn't want to tell me.
6. He can e-mail me what he wants to tell me.
7. He can help me do 301 permanent redirects.
8. He can tell me that someone is trying to invade my website or host

It sounds so Hyun and Hyun, okay, so let's talk about how he did it. Analysis by one:
1. We can use PHP to capture the user's source URL and the current URL, so the 1th we can do it very simply.
2. Because of the source URL, so you can very intuitive guess the user's intentions, I give an example, like my Site http://www.jb51.net, if a user's source URL is http://www.jb51.net, and capture to his current URL is http://www.jb51.net/user.php?id=1 This means that this person is browsing my site when clicked on a person's profile page when the results can not find this person's data and error, Description: id= 1 of this may not exist the goods have been deleted, then you have to look at the site is also a hyperlink to this URL, if you have to find a way to delete it!
3. This function is relatively simple, just need to use PHP to capture the other side of the IP can be. Then the function of the IP transfer Physical address of the other website can know where the person came from.
4. This function is very good, very powerful. Because my website www.7di.net is a server of my own configuration, so the security aspect must depend on oneself to handle, but often some not inflow of guy is interested in other people's server, they will use the program to try various methods to enter the server, And at this point you will find that you have received a large number of 404 errors in a short time, and the source URL is empty, they want to access the URL is some very sensitive URL, at this point you need to do is to open your firewall settings, and then clean and sealed off the IP. This is the last time I have used this method to seal dozens of IP, What a soldier! Forget to say, the Ministry of Information industry's IP has been blocked by me, I server in Taiwan you to verify what strength, really ~~!
5. To tell you the truth, this plugin just started to annoy me very much, because of his blessing I will receive hundreds of emails a day, some because of the user's normal browsing caused, some because the search engine to take a set of content caused, and some because of the small flat three want to invade the cause, in a word, later I changed slightly, The extension to meet the criteria or IP compliance are added to the validation, as long as these meet the conditions do not have to send email, so much more refreshing.
6. All this is out of the PHP instant email function, my approach is to modify the php.ini to implement the PHP internal mail function sent, of course, if you can also use other methods, such as: Install SendMail or develop a mail class to help third party SMTP send. As long as you enjoy it (the details are not the scope of today's discussion).
7. Since access to this page indicates that the URL that the user visited does not exist, so we have to guide users to access to the correct URL, so since it is to guide users to access the correct URL you may say that the direct use of a meta tag or a JS delay on the line why bother 301 turn? Because I do not want to be the search engine think I am cheating! The answer is complete.
8. I have already stated this in the above, basically for the small flat three judgment to be based on your experience and the familiarity of your own website to decide, even more, you have to generally understand what your site is the URL of the other site to the reverse chain (experience ah, not overnight can be overnight).

Should say, should put the end, the next play will be staged, the bottom of the code to save 404.php, and then Apache or Nginx and so on your 404 error page to point to come to the line.
Copy Code code as follows:

? Php
#设置URL, note no underline/
$MyURL = (IsSet ($_server[' http_host '))? Str_ireplace (' http://', ', ', Strtolower (RTrim ($_server[' http_host '), '/')): ' Www.7di.net ';
#设置URL, note no underline/
$MyDomain = (IsSet ($_server[' http_host '))? ' http://'. Str_ireplace (' http://', ', ', Strtolower (RTrim ($_server[' http_host '), '/')): ' Http://www.jb51.net ';
Set the time zone. Mainly used to correct 8-hour jet lag
Date_default_timezone_set (' etc/gmt-8 ');
Output Head
Header (' content-type:text/html; Charset=utf-8 ');
/**
Send e-mail
Parameters:
Recipients, message headers (no line breaks), message content (must be delimited between rows and rows, no more than 70 characters per line)
Description
Invoke PHP built-in function mail to send an email
Return:
Returns a Boolean value
Usage:
$IsSend =fun::mail2 ($email, $tit, $msg);
/**/
Function Mail2 ($to, $tit, $msg) {
IF (Filter_var ($to, filter_validate_email) = = ") {
throw new Exception (' Email address error! ');
}
$tit = ' =? UTF-8? B? '. Base64_encode ($tit). =';
$msg = Str_replace ("\ n.", "\ n..", $msg); Windows will be deleted if a period is found at the beginning of a line to avoid this problem by replacing a single period with two periods
Return Mail ($to, $tit, $msg, ' from:see7di@gmail.com '. \ n '. ' Content-type:text/html;charset=utf-8 ');
}
$msg = ' <table cellspacing= "0" cellpadding= "0" border= "0" ><tr><td style= "Padding:5px;background-color : #F57900; font-size:13px;border:2px solid #222; color: #222; " > ';
$msg. = (IsSet ($_server[' remote_addr '))? "<b> Visitor IP is: </b><br><a href= ' http://www.ip138.com/ips138.asp?ip={$_server[' REMOTE_ADDR ']} &action=2 ' target= ' _blank ' >{$_server[' remote_addr ']}</a><br> ': ';
$msg. = "<b> visit:</b><br>". Date (' y-m-d h:i:s ', Time ()). ' <br><br> ';
$msg. = (IsSet ($_server[' remote_host '))? "<a href= ' http://www.ip138.com/ips138.asp?ip={$_server[' remote_host ']}&action=2 ' target= ' _blank '" >{$_ server[' Remote_host ']}</a><br><br> ': ' <br> ';
$msg. = (IsSet ($_server[' http_user_agent '))? "<b> Visitor Details: </b><br>{$_server[' http_user_agent ']}<br><br>": ';
$msg. = (IsSet ($_server[' Request_uri '))? "<b> to visit the page is: </b><br>{$MyDomain}{$_server[' Request_uri ']}<br><br>": ";
$msg. = (IsSet ($_server[' http_referer ')) and Trim ($_server[' http_referer '])!= ')? "<b> Source address is: </b><br>{$_server[' Http_referer ']}<br><br>": ";
$msg. = ' </td></tr></table> ';
What types of URLs do not email, if you do not block out these words can annoy me
$arr =array (' mp3 ', ' rm ', ' swf ', ' jpg ', ' gif ');
Which IP does not send email, these are basically search engine spiders
$arrIP =array (' 66.249.77.217 ', ' 66.249.74.67 ');
The extension to access
$needEx =explode ('. ', Strtolower (Trim ($_server[' Request_uri ')));
$needEx =end ($needEx);
IF (! In_array ($needEx, $arr) and! In_array (Trim ($_server[' remote_addr '), $arrIP)) {
Mail2 (' see7di@gmail.com ', ' from '. $MyURL. ') "The 404 Error!", $msg);
}
UnSet ($MyURL, $msg, $needEx, $arr);
Header (' http/1.1 moved Permanently ');
Header ("location:{$MyDomain}");
Die ();

Note: If you feel that the functionality implemented in the top code does not satisfy your desires, then encourage you to rewrite him, improve it, strengthen him, I will not oppose, but please share your work to me, thank you ~~~~!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.