Apache Web server. htaccess file Configuration

Source: Internet
Author: User
Tags php and cpanel root directory

Htaccess is an Apache server configuration file, with powerful features, this article describes how to edit the file, let the Web site to block a country IP network segment, prevent pictures, file hotlinking, protect the directory and files under the host, create custom error page, redirect certain special IP address requests to other sites, and turn old domain names into new domain names, these features.

HtAccess file (hypertext Access file) is a very powerful configuration file for the Apache Web server, and for this file, Apache has a bunch of parameters that allow you to configure almost any arbitrary function.

Use. htaccess block out the search engine spider

Block Baidu search engine

Baidu is the legendary brute force search engine, do not follow the rules of search engine, if you try to use Robot.txt to block, that's very congratulations, absolutely ineffective. Therefore, it is necessary to use more violent and tough means to do so through. htaccess can be handled, if you want to block out some search engine (such as Baidu, Sogou, etc.), then please paste the following code into the Htaccess.txt file.

Setenvifnocase user-agent "^baidu" Bad_bot

Setenvifnocase user-agent "^sogou" Bad_bot

Setenvifnocase user-agent "^bloghoo" Bad_bot

Setenvifnocase user-agent "^scooter" Bad_bot

Deny from Env=bad_bot

Block out Google search engine

If you want to block the Google search engine, of course, you can also through the. Htaccsee block, as long as the input of the following code can be, if you want to be together with the violence Baidu search engine blockade, then put the two strings of code in the same Htaccsee.txt file.

Setenvif user-agent "^googlebot" Google

Deny from Env=google

Use. htaccess to block a country IP network segment

Sometimes it is possible to regulate the direction of the development of the site, there are not welcome certain countries to visit, the first idea should be to lock the country's IP, but do not know where to start? Because do not know that the IP of that country cannot lock, this time can through "blockcountry" The htaccess syntax generated by online tool sites blocks the IP segment countries that they want to block.

The 1th step to the Block Country (http://www.blockcountry.com) website homepage, first you will see a frame, the box has many countries, please choose to blockade of the country, you can check, select the completed click on the lower [block now!].

Step 2nd Pastes the code generated on the Web page into the htaccess file and stores it. Apache returns a 403 error for the rejected IP.

Use. htaccess to prevent pictures, files hotlinking

An illustrated website, the most fear is the article posted, although now has been respected copyright law, but still many people pretend not to see? Many people like "copy → Paste on", but such a pasting action will cause great harm to a website, in addition to hard to write the article was stolen, even the picture also hotlinking! Pictures or files stolen even the most fear is the subsequent flow, in order to avoid this problem, we must self-reliance, to prevent hotlinking this abhorrent behavior!

If it is cpanel virtual host, anti-theft method is simply to cpanel backstage to set. If you are other types of space backstage or that you only have FTP space, then what to do? In fact, as long as your space support. htaccess file and Support rewrite, you can prevent pictures and file Hotlinking!

Using the. htaccess to prevent the Web site's pictures, compressed files, or video and other non-HTML files are hotlinking is quite simple, by adding a few commands in the file to protect our valuable bandwidth. For example, the chinaz.com settings are as follows:

Rewriteengine on

Rewritecond%{http_referer}!^$ [NC]

Rewritecond%{http_referer}!nobing.cn [NC]

Rewritecond%{http_referer}!google.com [NC]

Rewritecond%{http_referer}!baidu.com [NC]

Rewritecond%{http_referer}!www.domain.com [NC]

Rewriterule. *. (gif|jpg) $ http://www.chinaz.com/no.png [r,nc,l]

Use. htaccess to protect directories and files under the host

In general, many virtual host presets are not open to protect the Web site host directory under the file, in fact, very dangerous, if you forget to place index files in the directory, it is likely that your directory is seen light, a careless very likely important information is taken away, this is quite a serious thing. If it is a Linux host, we can use the simple. htaccess syntax to protect the files in the host directory, so that others can not easily see.

Usually we use the simplest way to protect the directory, than the use of "index.php", "index.html" and so on the site index file, because the default set in the host in the directory is to grasp the index file, and the index file will usually have many kinds, The main opportunity to continue to arrange to crawl down, so you can set the index file to allow the host to crawl to achieve the effect of protecting the directory. But if the host needs no index file exists, then the host will directly display the contents of the directory, because the host does not know who to crawl as the main page, so will be all displayed.

Although it is possible to use the index file to protect the directory, each subdirectory needs to pass an index file, which is really tiring, so we can use ". htaccess" to batch processing.

Options-indexes

Advanced use of syntax

Of course, there are other related uses of the grammar, to the following a few simple items for your reference, if you think the following syntax is not you want or want to learn more, you can go to the official Apache website to see English instructions, see what else about "indexoptions" Use or other good features Oh!

Options +indexes: Show all files in the directory

Options-indexes: Hide all Files under the directory (described above)

Indexoptions +fancyindexing: Displays all the files in the directory with a small icon of the file type

Indexoptions-fancyindexing: Displays all files in the directory, but does not include a small diagram of the file type

Indexignore *.php *.exe: Hide specific files, the rest of the files are displayed normally (Hide all PHP and EXE files)

Use. htaccess to create a custom error page

This is a very simple thing for Linux Apache. Use the following. htaccess syntax you can easily complete this function. (Put the. htaccess in your site root directory)

ErrorDocument 401/error/401.php

ErrorDocument 403/error/403.php

ErrorDocument 404/error/404.php

ErrorDocument 500/error/500.php

redirect requests for specific IP addresses to another site

ErrorDocument 403http://www.youdomain.com

Order Deny,allow

Deny from all

Allow from 124.34.48.165

Allow from 102.54.68.123

Turn the old domain name into a new domain

# REDIRECT from old domain to new domain

Rewriteengine on

Rewriterule ^ (. *) $http://www.yourdomain.com/$1 [r=301,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.