Usage of Apache. htaccess file

Source: Internet
Author: User
Tags comments error handling html page php error php error log response code password protection

. Htaccess apache is not supported by default. To support it, open

1. How to make the local APACHE. htaccess

How to make the local APACHE & quot;. htaccess & quot? In fact, you only need to modify the httpd. conf settings of apache to make APACHE. htaccess simple. Let's take a look at the operation.

Open httpd. conf (where? In the CONF Directory of the APACHE Directory.
(1)
Options FollowSymLinks
AllowOverride None

Change
Options FollowSymLinks
AllowOverride All

(2) remove the following comments.
LoadModule rewrite_module modules/mod_rewrite.so


Put the following code into the. htaccess file in the corresponding directory to implement the corresponding functions.

Disable error display:

Php_flag display_startup_errors off
Php_flag display_errors off
Php_flag html_errors off
Php_value docref_root 0
Php_value docref_ext 0

Only PHP errors are displayed:

Php_flag display_errors on
Php_flag display_startup_errors on
Php_value error_reporting 2047

"2047" indicates the error level to be displayed. The detailed table is as follows:

1 E_ERROR
2 E_WARNING
4 E_PARSE
8 E_NOTICE
16 E_CORE_ERROR
32 E_CORE_WARNING
64 E_COMPILE_ERROR
128 E_COMPILE_WARNING
256 E_USER_ERROR
512 E_USER_WARNING
1024 E_USER_NOTICE
2047 E_ALL
2048 E_STRICT
4096 E_RECOVERABLE_ERROR

To save the error to the log file, you can set it as follows:

# Enable PHP error logging
Php_flag log_errors on
Php_value error_log/home/path/public_html/domain/PHP_errors.log

You can then disable access to the. log file:

# Prevent access to PHP error log

Order allow, deny
Deny from all
Satisfy All

Set the maximum size of the error log, in bytes:

# General directive for setting max error size
Log_errors_max_len integer

Based on the above, the PHP error display settings for. htaccess are summarized as follows:

# PHP error handling for production servers

# Disable display of startup errors
Php_flag display_startup_errors off

# Disable display of all other errors
Php_flag display_errors off

# Disable html markup of errors
Php_flag html_errors off

# Enable logging of errors
Php_flag log_errors on

# Disable ignoring of repeat errors
Php_flag ignore_repeated_errors off

# Disable ignoring of unique source errors
Php_flag ignore_repeated_source off

# Enable logging of php memory leaks
Php_flag report_memleaks on

# Preserve most recent error via php_errormsg
Php_flag track_errors on

# Disable formatting of error reference links
Php_value docref_root 0

# Disable formatting of error reference links
Php_value docref_ext 0

# Specify path to php error log
Php_value error_log/home/path/public_html/domain/PHP_errors.log

# Specify recording of all php errors
Php_value error_reporting 999999999

# Disable max error string length
Php_value log_errors_max_len 0

# Protect error log by preventing public access

Order allow, deny
Deny from all
Satisfy All

The following settings are suitable for developers:

# PHP error handling for development servers
Php_flag display_startup_errors on
Php_flag display_errors on
Php_flag html_errors on
Php_flag log_errors on
Php_flag ignore_repeated_errors off
Php_flag ignore_repeated_source off
Php_flag report_memleaks on
Php_flag track_errors on
Php_value docref_root 0
Php_value docref_ext 0
Php_value error_log/home/path/public_html/domain/PHP_errors.log
Php_value error_reporting 999999999
Php_value log_errors_max_len 0

Order allow, deny
Deny from all
Satisfy All


1. Locate the error document

(1) common client request error return code:

401 Authorization Required 403 Forbidden 404 Not Found 405 Method Not Allowed 408 Request Timed Out 411 Content Length Required 412 Precondition Failed 413 Request Entity Too Long 414 Request URI Too Long 415 Unsupported Media Type (2) common server error response code:

500 Internal Server Error (3) users can use. htaccess to specify the Error reminder page they have prepared in advance. Generally, you can set up a directory, such as errors, to place these pages. Then, add the following command to. htaccess:

ErrorDocument 404/errors/notfound.html ErrorDocument 500/errors/internalerror.html one command line.
The first instruction above indicates that for 404, that is, when the required document is not found, the page displays the notfound.html page under the/errorsdirectory. The syntax format is as follows:

ErrorDocument error code/directory name/file name. Extension if there is little information to be prompted, you do not have to create a page and use the HTML number directly in the command. For example:

ErrorDocument 401 "<body bgcolor = # ffffff>
You are not authorized to access this page!
</Body>"
2. Document access password protection: to use. htaccess to set the access user and password for documents in a directory, you must first generate a. htpasswd text document, for example:

Zheng: y4E7Ep8e7EYV the password is encrypted here. You can find some tools to encrypt the password to the encoding supported by. htaccess. This document should not be stored in the www Directory. It is recommended that it be stored outside the www root directory file, which is safer.

With the authorization user documentation, you can add the following commands to. htaccess:

AuthUserFile. the server directory of htpasswd AuthGroupFile/dev/null (the directory to be accessed) AuthName EnterPassword AuthType Basic (authorization type) require user wsabstract (users allowed to access, if you want to allow all users in the table, you can use require valid-user)

Note: The brackets are the comments you have added during learning.

3. Deny access from an IP address

If I don't want a government department to access the content of my site, I can add the department's IP address in. htaccess to reject them. For example:

Order allow, deny from 210.21.112.43deny from 219.146.95 allow from all note: the second row rejects an IP address, and the third row rejects an IP segment, that is, 219.146.95.0 ~ 219.146.95.255 want to reject all users? Use deny from all. You can use a domain name instead of an IP address.

IV. Protection of. htaccess documents

When you use. htaccess to set password protection for a directory, it contains the path of the password file. In terms of security, it is necessary to protect. htaccess from being visible to others. Although this can be done in other ways, such as document permissions. However,. htaccess can also be achieved by adding the following command:

<Files. htaccess> order allow, deny from all </Files> 5. URL redirection

We may have re-planned the website, migrated the document, or changed the directory. In this case, access from the seo/seo.html "target =" _ blank "> search engine or other website links may fail. In this case, you can use the following command to automatically redirect the old URL to the new address:

Redirect/old directory/old document name the address of the new document or the redirection of the entire Directory: the new directory of the old Redirect directory changes the default homepage file. Generally, the default homepage file names include default and index. However, sometimes there is no default file in the directory, but a specific file name, such as pmwiki. php in pmwiki. In this case, it is very difficult for users to remember the file name for access. You can easily set a new default file name in. htaccess:

The new default file names of DirectoryIndex can also be listed in multiple order, indicating the priority between them,

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.