The configuration tips for the. httacces file _linux

Source: Internet
Author: User
Tags php script
1. Configure. htaccess Custom Error page
The first application of the. htaccess is to customize the error page, which will allow you to have your own, personalized error page (for example, when you can't find a file), not the error page that your service provider offers or no pages. This will make your site look more professional when it goes wrong. You can also use a script to notify you when something is wrong (for example, I use the PHP script for free webmaster Help, and automatically email me when I can't find the page).

Any page error code you know (like 404 can't find a page) can be made into a custom page by adding the following text to the. htaccess file:

ErrorDocument errornumber/file.html

For example, if I have a nofound.html file under my root directory, I want to use it as a 404 error page:

ErrorDocument 404/notfound.html

If the file is not in the root directory of the site, you only need to set the path to:

ErrorDocument 500/errorpages/500.html

Here are some of the most common errors:

Common Client Request error return code:

400-bad Request Error Requests
401 Authorization required Need verification
403 Forbidden Prohibited
404 Not Found cannot find the page
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:

The Internal server error internal server errors

Next, all you have to do is create a file that appears when the error occurs, and then upload it with the. htaccess.

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 "You do not have access to this page, please give up!" ”

2. Configure. htaccess Deactivate display directory list
Sometimes, for some reason, there is no index file in your directory, which means that when someone has typed the path to the directory in the browser's address bar, all the files in that directory will be displayed, which will leave a security risk to your site.

To avoid this situation (without having to create a bunch of new index files), you can type the following command in your. htaccess document to block the display of directory listings:
Options-indexes

3. Configure. htaccess block/allow specific IP address
In some cases, you may want to allow only certain IP users to access your site (for example, only users of a particular ISP are allowed to enter a directory), or you might want to block certain IP addresses (for example, isolating low-level users from your information layout). Of course, this is only useful when you know the IP address you want to intercept, but most users on the Web now use dynamic IP addresses, so this is not a common way to restrict usage.

You can use the following command to block an IP address:

Deny from 000.000.000.000

The 000.000.000.000 here is the banned IP address, and if you specify only a few of them, you can block the entire network segment's address. If you enter 210.10.56, you will ban all IP addresses for 210.10.56.0~210.10.56.255.

You can use the following command to allow an IP address to access a Web site:

Allow from 000.000.000.000

The allowed IP address is 000.000.000.000, and you can block the entire network segment like a banned IP address.

If you want to prevent everyone from accessing the directory, you can use:

Deny from all

This does not, however, affect the scripting program's use of documents in this directory.

4. Configure. htaccess Replace the index file
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 W3sky in W3sky. Php. 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

Maybe you don't want to always use index.htm or index.html as the index file for the directory. For example, if your site uses PHP files, you might want to use index. PHP as the index document for the directory. Of course, you don't have to limit yourself to "index" documents, if you want to, use. htaccess you can even set up Foofoo.balh to be your index document!

These alternate index files can be lined up in a list, and the server searches from left to right to check which documents exist in the real directory. If one is not found, it will display the list of directories (unless you have turned off the list of directory files).

DirectoryIndex index. PHP index. PHP3 messagebrd.pl index.html index.htm

5. Configure. htaccess redirect page
One of the most useful features of htaccess is redirecting requests to different documents within or outside the station. This becomes extremely useful when you change a file name, but still want users to access it with their old address. Another application (which I found useful) is to redirect to a long URL, for example, in my newsletter, I can use a very short URL to point to my affiliate link. The following is an example of a redirected file:

Redirect/location/from/root/file.ext
Http://www.w3sky.com/new/file/123.html

In the example above, access to the root directory named oldfile.html can be typed:

/oldfile.html

To access a file in an older subdirectory, you can type:

/old/oldfile.html

You can also use the. htaccess to redirect the entire Web site's directory. If you have a directory named OldDirectory on your site, and you have created the same document on a new website http://www.w3sky.com/newdirectory/, You can redirect all files in the old directory without one by one statements:

Redirect/olddirectory http://www.w3sky.com/newdirectory

In this way, any request to the/olddirectory directory in the site will be pointed back to the new site, including additional URL information. For example, someone type:

Http://www.w3sky.com/olddirecotry/oldfiles/images/image.gif

The request will be redirected to:

Http://www.w3sky.com/newdirectory/oldfiles/images/image.gif

If used correctly, this feature will be extremely powerful.

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

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.