PHP Basics: A detailed explanation of the function of the Apache. htaccess file

Source: Internet
Author: User
Tags error code html page php basics require domain name password protection

the function of the. htaccess file in Apache

-The. htaccess file (or distributed configuration file) provides a way to configure changes to the directory, that is, to place a file containing one or more directives in a specific document directory to function in this directory and all its subdirectories. As a user, the commands you can use are limited. The administrator can set it through the Apache allowoverride directive.

-directives in subdirectories overwrite instructions in higher-level directories or primary server configuration files.

-. htaccess must be uploaded in ASCII mode, preferably with the permissions set to 644.

locating the wrong document

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

Common Server error return code:

Internal Server Error

Users can use. htaccess to specify their own prepared Error alert page. In general, people can set up a directory specifically, such as errors to place these pages. And then again. htaccess, add the following instructions:

ErrorDocument 404/errors/notfound.html
ErrorDocument 500/errors/internalerror.html

A single line of instructions. The first instruction above means that the page is displayed as a notfound.html page in the/errors directory for 404, which is when the required document is not found. It is not difficult to see that the syntax format is:

errordocument error code/directory name/filename. extension

If you need to be prompted with very little information, you don't have to create a page to use the HTML number directly in the instruction, such as the following example:

ErrorDocument 401 "<body bgcolor= #ffffff >

password protection for document Access

To use. htaccess to set access to the user and corresponding password for a document in a directory, the first thing to do is to generate a. htpasswd text document, such as:

Zheng:y4e7ep8e7eyv

The password is encrypted, and the user can find some tools to encrypt the password into a. htaccess supported encoding. This document is best not to be placed in the WWW directory, it is recommended to be placed outside the WWW root document, which is more secure.

With the authorized user documentation, you can add the following directive to the. htaccess:

Server directory for AuthUserFile. htpasswd
Authgroupfile/dev/null (directories that require authorization to access)
AuthName Enterpassword
AuthType Basic (Authorization type)

Require user wsabstract (users who are allowed to access, you can use require valid-user if you want all users in the table to allow it)

Note, brackets part for the time of learning to add their own notes

deny access from an IP

If I don't want a government department to access the content of my site, I can reject it by joining the department's IP in the. htaccess.

For example:

Order Allow,deny
Deny from 210.21.112.43
Deny from 219.146.95
Allow from all

The second line rejects an IP, and the third line rejects an IP segment, which is 219.146.95.0~219.146.95.255

Want to reject everyone? With the Deny from all. Not only with IP, you can also use domain name to set.

protect. htaccess documents

When you use. htaccess to set the password protection for a directory, it contains the path to the password file. From a security point of view, it is necessary to protect the htaccess, and not let others see the content. Although this can be done in other ways, such as the permissions of the document. However, htaccess itself can do so by simply adding the following instructions:

<files .htaccess>
Order Allow,deny
Deny from all
</Files>

URL Steering

We may be planning the site, migrating the document, or changing the directory. At this point, access from the search engine or other web links may be wrong. In this case, the old URL can be automatically shifted to the new address by using the following instructions:

Redirect/old directory/old document name new document address

or the entire directory of the steering:

Redirect Old Directory New directory

Change the default home file

Under normal circumstances, the default home page name has default, index and so on. However, there are times when there are no default files in the directory, but a specific file name, such as pmwiki.php in PmWiki. In this case, it is troublesome for the user to remember the filename to access. You can easily set a new default filename in. htaccess:

DirectoryIndex the new default filename

You can also list multiple, sequentially indicating the priority level between them, for example:

DirectoryIndex filename.html index.cgi index.pl default.htm

Prevent hotlinking

If you do not like others to connect their own pictures and documents on their Web pages, you can also do so by htaccess instructions.

The required directives are as follows:

Rewriteengine on
Rewritecond%!^$
Rewritecond%!^http://(www.)? webjx.com/.*$ [NC]
Rewriterule. (gif|jpg) $-[F]

If you feel that letting someone else's page open a skylight doesn't look good, you can use a picture instead:

Rewriteengine on
Rewritecond%!^$
Rewritecond%!^http://(www.)? mydomain.com/.*$ [NC]
Rewriterule. (gif|jpg) $ http://www.webjx.com/img_PageNotFound.gif [r,l]



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.