Detailed Apache under. Htaccess file commonly used configuration

Source: Internet
Author: User
Tags access address advanced anti- apache block cache change

The .htaccess file is one of the most commonly used configuration files for Apache servers and is responsible for web page configuration in related directories. By htaccess file, can help us to achieve: page 301 redirect, custom 404 error page, change the file extension, allow / block specific users or directory access, directory banned, configure the index entry and other functions.

In addition, .htaccess manual editing method is to use an advanced text editor such as UE or notepad2. If you do not facilitate the download or manual preparation of the file is wrong, it is highly recommended online htaccess text editor, this editor has been very perfect, and in the secondary development has added a lot of new features, easy to use, the perfect support for multiple conditions Complex task htaccess file intelligence generation task.

1, open the site's picture anti-hotlinking function

If the site's traffic is limited, then the picture anti-hotlinking is very necessary. Not only can you reduce the waste of traffic, but you can also reduce the server load. Open the picture anti-hotlinking function need to add the following code in the htaccess file:

RewriteEngine on
RewriteCond% {HTTP_REFERER}! ^ $
RewriteCond% {HTTP_REFERER}! Http: // (www.)? Allow1.com (/)?. * $ [NC]
RewriteCond% {HTTP_REFERER}! Http: // (www.)? Allow2.com (/)?. * $ [NC]
RewriteRule. *. (Gif | jpg | jpeg | bmp | png) $ [R, NC, L]

In the code above, allow1.com and allow2.com are the domain names that allow access to the site image, and the baidu.com behind is the target image address that is replaced. Above also contains the picture file suffix parameters, you can change. If generated by htaccess online generator this code, just need to fill in a few allow access to the domain name (without www and http), and then fill in the image address to be replaced.

2, custom 404,500 error return page

If you do not want the website can not find the page when "404 page can not be displayed", try to define a page that can not find the error page! Need to add the htaccess file

ErrorDocument 404 /404.htm

The front of the 404 is the error code, followed by the / 404.htm is its own definition of the 404 page path. Note that if the 404 page size is less than 512B, IE will ignore this page, it will not take effect. The following parameters need to use the site relative path. In the htaccess online generator, please switch to the "custom error page" tab, and then in the "404 (not found)" input box enter your error page address. The online build tool can also handle other types of errors, and so on. Please pre-404 wrong pages made to upload to the site directory.

3, shield website directory list

Directory list is the site can not find the default file emergency measures, the server will be unreserved to return all the files in a current directory. This will give the site a very big security risk. Therefore, many programs now have an index.htm file under any subdirectory to avoid such omissions. However, through htaccess file we can completely solve this problem.

Add the following code in the file:

Options -Indexes

This will completely shut down the site's directory listing functionality, but it will have no impact on the normal access to the site. If you want to display the file list, you only have to change "-" to "+". If you use the htaccess online generation tool, you just need to select the "whether to display the list of files without the default document list" menu to "hide", the program will automatically generate the relevant code.

4, WWW unified web site format and redirect

In the view of SEO, if abcd.com and www.abcd.com can access it, this is a very bad experience for search engines, and it is easy to cause the decentralization. And what we have to do is to unify the website with www and the website without www.

We do not recommend to manually write the code, automatically generated program has done a sound judgment, you only need to switch to the "WWW domain name redirection" tab, enter the address bar in the WWW domain name redirection, you want to eventually use Domain name! Note that the domain name entered here must be a full web address, such as http://www.xxxx.org or http://xxxx.org. Based on your input, the program will automatically determine whether to use redirect or redirect.

5, the default entry page

Some virtual hosts are alternative, do not apply the default file name, or non-mainstream order. Then by the following code you can define the page name of the paper. For example index.htm, default.php, etc.:

DirectoryIndex index.php

This line of code will index.php as the default home page. Other pages please directly replace the index.php example. If your site has multiple pages that need to be your homepage, write the file in the format DirectoryIndex index.php index2.php. This setting in the htaccess generator, just need to fill in the home page followed by the file name, very convenient.

6, through htaccess open site image cache

Some virtual hosts limit the site's traffic, and wasting significant amounts of bandwidth if browsers download the most common image files from the server each time. So you can open the image cache, the browser will automatically load the image file from the cache before the cache expires. This function in the file by adding the following code:

Header set Cache-Control "max-age = 3600"

Note that the above 3600 units are seconds, that is, the timeout period. In the above figure, gif is the suffix of the picture file, which means that these files take effect. If you use the auto-generation tool, to turn this option on, select a timeout for the On Site's Image Cache tab. There are currently 10 minutes, 1 hour, 1 day options available for setup.

7, fixed 301,302 webpage redirect

The 404 error due to the page transfer location is a big headache for all of us, but we only have to do a 301 redirect to completely solve the problem. Add the following code to the htaccess file:

Redirect permanent /old.htm http://www.jzread.com

The first parameter using the relative path, you can also use the directory name, the latter parameter must fill in the full URL. This will take effect without any mistakes. In SEO, search engines will soon follow your settings, will be included in the updated pages.

In addition, htaccess also can be such as MIME file type changes, domain name redirection and other functions, therefore, make full use of the file will make your website more handy.

Related Article

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.