PHP custom 404 error

Source: Internet
Author: User
Tags php source code

Introduction: This is a detailed page for PHP custom 404 errors. It introduces PHP related knowledge, skills, experiences, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 322981 'rolling = 'no'>

I have been developing a website and have never known how to customize the 404 error page in PHP. Today I finally figured it out, which is very simple.
    1. Create a self-built 404 error page in the WWW file in the PHP environment. I defined it as 404.php which contains some friendly pictures.
    2. Create a. htaccess file with the content errordocument 404/404/404 .php (custom 404 error page address for you), Save.
    3. Check whether the rewrite module of your Apache server is enabled. In HTTP. conf, find loadmodule rewrite_module modules/mod_rewrite.so, remove the previous #, save and restart the server.
    4. Now you can open an interface that does not exist to display your custom 404 error page.

At the beginning, I did not know why the image was not displayed. Later I realized that the relative path was incorrect. Finally, I changed it to absolute path, which means the image was correctly displayed.

As a matter of fact, it was later known that the original. htaccess and so many error pages can be customized.

Here is the transfer: http://hi.baidu.com/designwow/blog/item/c82a6edc49bb9de176c638bd.html

The. htaccess file is a very powerful configuration file of the Apache Web server. For this file, Apache has a bunch of parameters that allow you to configure almost anything you want .. The htaccess configuration file adheres to a Unix culture-using an ASCII plain text file to configure your website access policy.

This articleArticleIt includes 16 very useful tips. In addition, because. htaccess is a very powerful configuration file. Therefore, a slight syntax error may cause your entire website to fail. Therefore, when you modify or replace the original file, make sure to back up the old files so that they can be easily restored in case of problems.

1. Use. htaccess to create a custom error page.This is an extremely simple task for Linux Apache. You can use the following. htaccess syntax to easily complete this function. (Put. htaccess in the root directory of your website)

Errordocument 401/error/401.php
Errordocument 403/error/403.php
Errordocument 404/error/404.php
Errordocument 500/error/500.php

2. Set the time zone of the website

Setenv TZ America/Houston

3. Blocked IP list
Sometimes, you need to block some access by IP address. Whether it is an IP address or a network segment, this is a very simple task, as shown below:

Allow from all
Deny from 145.186.14.122
Deny from 124.15

Apache returns the 403 error for the rejected IP address.

4. transfer some old links to new links-Search Engine Optimization SEO

Redirect 301/D/file.html http://www.htaccesselite.com/r/file.html

5. Set email for the server administrator.

Serversignature email
Setenv server_admin default@domain.com

6. Use. htaccess to access and Stop leeching.If an image on your website is referenced by more than N other websites, this may lead to performance degradation on your server.CodeIt can protect some popular links from being referenced too much.

Options + followsymlinks
# Protect hotlinking
Rewriteengine on
Rewritecond % {http_referer }! ^ $
Rewritecond % {http_referer }! ^ Http: // (www .)? Domainname.com/[Nc]
Rewriterule. *. (GIF | JPG | PNG) $ http://domainname.com/img/hotlink_f_o.png [Nc]

7. Block all requests from the user agent

#. Htaccess code: Begin
# Block bad bots by User-Agent
Setenvifnocase User-Agent ^ FrontPage [NC, or]
Setenvifnocase User-Agent ^ java. * [NC, or]
Setenvifnocase User-Agent ^ Microsoft. url [NC, or]
Setenvifnocase User-Agent ^ msfrontpage [NC, or]
Setenvifnocase User-Agent ^ offline. Explorer [NC, or]
Setenvifnocase User-Agent ^ [WW] EB [BB] andit [NC, or]
Setenvifnocase User-Agent ^ Zeus [Nc]

Order allow, deny
Allow from all
Deny from Env = bad_bot

#. Htaccess code: End

8. Redirect requests from some special IP addresses to other sites

Errordocument 403 http://www.youdomain.com
Order deny, allow
Deny from all
Allow from 124.34.48.165
Allow from 102.54.68.123

9. open the file instead of downloading it.In general, when we open an online file, a dialog box is always displayed asking us whether to download or directly open the file. This problem will not occur if we use the following settings. open the file directly.

Addtype application/octet-stream. PDF
Addtype application/octet-stream. Zip
Addtype application/octet-stream. mov

10. Modify the file typeThe following example allows any file to be interpreted by the server as PHP. For example, myphp, CGI, and phtml.

Forcetype application/X-httpd-PHP
Sethandler application/X-httpd-PHP

11. block access to. htaccess files

# Secure htaccess File

Order allow, deny
Deny from all
12. Protect files on the server from being accessed

# Prevent access of a certain file order allow, deny
Deny from all
13. Blocking directory browsing

# Disable directory browsing
Options all-Indexes

14. Set the default homepage

# Serve alternate default index page
Directoryindex about.html

15. Password AuthenticationYou can create a file for authentication. The following is an example:

# To protect a file

Authtype basic
Authname "prompt"
Authuserfile/home/path/. htpasswd
Require valid-user

# Password-protect a directory
Resides
Authtype basic
Authname "this directory is protected"
Authuserfile/home/path/. htpasswd
Authgroupfile/dev/null
Require valid-user

16. convert old domain names to new ones

# Redirect from old domain to new domain
Rewriteengine on
Rewriterule ^ (. *) $ http://www.yourdomain.com/#1 [R = 301, l]

Example:
I want to create another website under the IP address 127.0.0.1. The original website is www.abc.com.
Now I want to create another website www.123.com.
So I first created a 123 folder in the root directory of www.abc.com, and then created a. htaccess file.

Directoryindex default. php index.htm index. php index.html default.htm default.html index.shtml default.shtml
Rewriteengine on
Options all-Indexes
Php_value upload_max_filesize 20 m
Rewriteengine on
Rewritecond % {http_host} ^ (www .)? 123. com $
Rewritecond % {request_uri }! ^/123/
Rewritecond % {request_filename }! -F
Rewritecond % {request_filename }! -D
Rewriterule ^ (. *) $/123/$1
Rewritecond % {http_host} ^ (www .)? 123. com $
Rewriterule ^ (/)? $123/index. php [l]
Errordocument 401/404 .htm
Errordocument 403/404 .htm
Errordocument 404/404 .htm
Errordocument 500/404 .htm

Directoryindex default. php index.htm index. php index.html default.htm default.html index.shtml default.shtml
This defines the default homepage.
Rewriteengine on
Options all-Indexes
Php_value upload_max_filesize 20 m

This is ModificationProgramUpload File Size

Rewriteengine on
Rewritecond % {http_host} ^ (www .)? 123. com $ this is a domain name
Rewritecond % {request_uri }! ^/123/This is the directory settings
Rewritecond % {request_filename }! -F
Rewritecond % {request_filename }! -D
Rewriterule ^ (. *) $/123/$1 directory settings
Rewritecond % {http_host} ^ (www .)? 123. com $ Domain Name
Rewriterule ^ (/)? $123/index. php [l] default access file, which can be changed to another
Errordocument 401/404 .htm
Errordocument 403/404 .htm
Errordocument 404/404 .htm
Errordocument 500/404 .htm
These are error document definitions.
The. htaccess file is powerful and can implement many functions. You can even define PHP parameters, such as the file size to be uploaded and the PHP program running time. As long as you are good at exploring, You can implement more functions.

More articles about "php custom 404 error"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/322981.html pageno: 16.

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.