The. htaccess file is the most commonly used configuration file on the Apache server. It is used to configure web pages under the relevant directory. The htaccess file can help us achieve: webpage 301 redirection, custom 404 error pages, changing file extensions, allowing/blocking access to specific users or directories, prohibiting directory lists, and configuring index entries.
In addition, the manual editing method of. htaccess is to use advanced text editors such as UE or notepad2. If it is not convenient to download or manually compile a file, we also recommend the online htaccess text editor. Currently, this editor is very complete and has added many new features in secondary development, which are easy to use, supports the intelligent generation of htaccess files for complex tasks with multiple conditions.
1. Enable the image anti-leech function of the website
If the website traffic is limited, anti-leech protection is necessary for images. This not only reduces traffic waste, but also reduces the burden on servers. To enable the image anti-leech function, add the following code to 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 above Code, allow1.com and allow2.com are the domain names that allow access to website images, and Baidu.com is the destination image address. The above also contains the suffix parameter of the image file, which can be changed by yourself. If you use the htaccess online builder to generate this code segment, you only need to fill in a few domain names that are allowed to access (without www or HTTP), and then fill in the address of the image to be replaced.
2. Customize Error Response pages such as 404 and 500
If you do not want the website to display "404 this page cannot be displayed" when it cannot be found, try to define an error page that cannot be found! Add the following content to the htaccess file:
Errordocument 404/404 .htm
The preceding 404 is the error code, and the following/404.htm is the path of the custom 404 page. It should be noted that if the size of this 404 page is smaller than B, ie will ignore this page and it will not take effect. The following parameters must use the relative website path. In the online generation program of htaccess, switch to the "custom error page" tab and enter your error page address in the input box after "404 (not found. The online generation tool can also handle other types of errors, and so on. Make a 404 error page and upload it to the website directory in advance.
3. List of blocked website Directories
The directory list is an emergency action when the website cannot find the default file. The server returns all files in the current directory without any reservation. This poses a great security risk to the website. For this reason, many programs now put an index.htm file under any directory to avoid this omission. However, we can completely solve this problem through the htaccess file.
Add the following code to the file:
Options-Indexes
This will completely disable the directory list function of the website, but it does not affect normal Website access. To display the file list, you only need to change "-" to "+ ". If you use the htaccess online generation tool, you only need to select "hide" from the "show file list in directories without default documents" menu, and the program will automatically generate relevant code.
4. Unified www url format and redirection
In Seo's opinion, if both abcd.com and www.abcd.com can be accessed, this is a very bad experience for search engines and can easily cause scattered weights. What we need to do is to unify the websites with and without www.
We do not recommend that you manually write this section of code. The automatically generated program has been fully judged. You only need to switch to the "www domain redirection" tab, enter the domain name you want to use in the address bar after WWW Domain Name Redirection! Note that the domain name entered here must be a complete URL, such as a http://www.xxxx.org or a http://xxxx.org. The program will automatically determine whether to use forward redirection or reverse redirection based on your input.
5. Default portal page
Some virtual hosts are alternative and do not apply to default file names or non-mainstream orders. The following code defines the file name of the page. Such as index.htm and default. php:
Directoryindex index. php
This line of code uses index. php as the default homepage. Replace index. php on other pages. If the website has multiple pages that need to be used as the homepage, write files in the format of directoryindex index. php index2.php. In the htaccess generator, you only need to fill in the names of the homepage in sequence, which is very convenient.
6. Enable website image caching through htaccess
Some virtual hosts limit website traffic. If the browser downloads the most common image files from the server each time, it will waste a lot of bandwidth. Therefore, you can enable image caching. the browser will automatically load image files from the cache before the cache expires. Add the following code to this function:
Header set cache-control "Max-age = 3600 ″
Note that the above 3600 unit is seconds, that is, the timeout time. In GIF, etc., the image file suffix indicates that these files are effective. If you use the automatic generation tool to enable this option, select a time-out period on the "enable site image cache" tab. Currently, you can set ten minutes, one hour, or one day.
7. Fixed 301 and 302 Page redirection
The 404 error caused by PAGE transfer location is a headache for all of us, but we only need to make a 301 redirection to completely solve this problem. Add the following code to the htaccess file:
Redirect permanent/old.htm http://www.php100.com
The previous parameter uses a relative path or a directory name, which must be filled in with the complete URL. This will take effect without any errors. In Seo, the search engine will soon update the included webpage according to your settings.
In addition, htaccess can also implement functions such as modifying the MIME file type and Domain Name Redirection. Therefore, making full use of this file will make your website easier to build.