Home>Article>Backend Development> How to rewrite php 404 error

How to rewrite php 404 error

藏色散人
藏色散人 Original
2020-07-13 09:11:23 2315browse

php 404 error rewriting method: first set the URL and set the time zone; then call the PHP built-in function Mail to send the email; then set the extension to be accessed; finally set the relevant code file to "404.php "That's it.

How to rewrite php 404 error

Use PHP to rewrite the 404 error page

404 error, many people know that if you want to access the url This page is read and displayed when it does not exist. In the past, our usual approach to dealing with 404 was to simply write a few lines of text, and some interested people might beautify it a little, and a small number of people who want to take advantage of it may even Use meta tags to do delayed redirection, that's all. In fact, 404 can also help us do a lot of things. This is the focus of what we are going to discuss today.

First of all, let me explain what I mean. What exactly can 404 do for me:

1. He can tell me when and where users want to access my URL and the result fails.

2. He can tell me why the other party wants to visit this URL and help me optimize the website

3. He can tell me the other party’s IP address

4. He can tell me what this person’s intentions are

5. I can ask him to shut up what he doesn’t want to tell me

6. He can send me what he wants to tell me via email

7. He can help me make a 301 permanent redirect.

8. He can tell me that someone is trying to invade my website or host

It sounds very mysterious and cool, okay, let’s continue Next, let’s discuss how he did it. Analyze one by one:

1. We can use PHP to capture the user’s source URL and current URL, so the first point we It can be done very easily.

2. Because of the source URL, you can intuitively guess the user's intention. Let me give you an example, just like mine Website http://lailinlin.com, if the source URL of a user is http://lailinlin.com, and the current URL captured is http://lailinlin.com/user.php?id=1, then It means that when this person browsed my website and clicked on a person's personal information page, he could not find the person's information and made an error. It means: the product with ID=1 may not exist and has been deleted, then you Just check to see if there is a hyperlink to this URL on the website. If so, find a way to delete it!

3. This function is relatively simple. , you only need to use PHP to capture the other party's IP. Then you can know where the person comes from by using the IP to physical address function of other websites.

4. This function is very useful Okay, very powerful. Because my website http://lailinlin.com is a self-configured server, I have to handle the security aspects by myself, and there are often some unscrupulous guys who are interested in other people's servers and they will use programs. Try various methods to enter the server, and at this time you will find that you receive a large number of 404 errors in a short period of time, and the source URL is empty. The URLs they want to access are all very sensitive URLs. At this time, you need to All you need to do is open your firewall settings, and then block this IP cleanly. Recently, I have used this method to block dozens of IPs. It’s really a small effort that makes a big difference! I forgot to mention that the IP of the Ministry of Information Industry is also I blocked it. My server is in Taiwan. Why are you trying to verify it? Really! Thanks to him, I receive hundreds of emails every day, some of which are caused by users browsing normally, some of which are caused by various search engines collecting content, and some of which are caused by bad guys who want to invade. In short, I don’t tire of it, and later I made a slight change and added all the extensions that meet the conditions or IPs that meet the conditions to the verification. As long as these meet the conditions, there is no need to send emails, which makes it much more refreshing.

6 . All of this is inseparable from PHP's ability to send emails instantly. My approach is to modify PHP.INI to implement PHP's internal mail function for sending. Of course, you can also use other methods, such as installing sendmail or developing a mail class. You can send it with the help of a third-party SMTP. As long as you feel comfortable (the specific details are not the scope of today’s discussion).

7. Since this page can be accessed, it means that the URL visited by the user is Doesn't exist, so we have to guide users to access the correct URL. So since we are guiding users to access the correct URL, you might say that just using a meta tag or a JS delay is enough. Why bother with a 301 redirect? ?Because I don’t want search engines to think I’m cheating! Answer completed.

8. I have already explained this point above. Basically, the judgment of the little bastard should be based on It depends on your experience and familiarity with your own website. What's more, you need to have a general understanding of which URLs on your website are backlinked by other websites (experience, it cannot be achieved overnight).

What should be said and what should be said is over. The show is about to be staged next. Save the code below as 404.php, and then use apache or nginx to save your 404 error. Just point to the page.

'; $msg.=(isSet($_SERVER['REMOTE_ADDR'])) ? " 來訪者IP是:
{$_SERVER['REMOTE_ADDR']}
" : ''; $msg.="來訪時間:
".date('Y-m-d H:i:s',time()).'

'; $msg.=(isSet($_SERVER['REMOTE_HOST'])) ? "{$_SERVER['REMOTE_HOST']}

" : '
'; $msg.=(isSet($_SERVER['HTTP_USER_AGENT'])) ? "來訪者詳細資料:
{$_SERVER['HTTP_USER_AGENT']}

" : ''; $msg.=(isSet($_SERVER['REQUEST_URI'])) ? "要訪問的頁面是:
{$MyDomain}{$_SERVER['REQUEST_URI']}

" : ''; $msg.=(isSet($_SERVER['HTTP_REFERER']) And Trim($_SERVER['HTTP_REFERER'])!='') ? "來源地址是:
{$_SERVER['HTTP_REFERER']}

" : ''; $msg.=''; //哪些類型的URL不發Email,如果不屏蔽掉這些的話能煩死我 $arr=Array('mp3','rm','swf','jpg','gif'); //哪些IP不發Email,這些基本都是搜索引擎的蜘蛛 $arrIP=Array('66.249.77.217','66.249.74.67'); //要訪問的擴展名 $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','來自【'.$MyURL.'】的404錯誤!',$msg); } unSet($MyURL,$msg,$needEx,$arr); Header('HTTP/1.1 301 Moved Permanently'); Header ("Location:{$MyDomain}"); Die();

For more related knowledge, please visit

PHP Chinese website

!

The above is the detailed content of How to rewrite php 404 error. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Why is php cold? Next article:Why is php cold?