. htaccess Rules

Source: Internet
Author: User
Tags 403 forbidden error password protection

. htaccess Rules

1. Introduction Introduction
File name. htaccess Property 644 (rw-r–r–)
Htaccess will affect all subdirectories in the directory where it resides
Note that most of the content is required to remain within a line, do not wrap, or cause errors

2. Error documents
Official document:errordocument directive
ErrorDocument Code Document
Example
ErrorDocument 400/errors/badrequest.html
ErrorDocument 404 http://yoursite/errors/notfound.html
ErrorDocument 401 "Authorization Required"

More..
Less.. (Note that the double quotes that appear after this will need to be escaped as ")
Common HTTP status Codes
Successful Client Requests
OK
201 Created
Accepted
203 non-authorative Information
204 No Content
205 Reset Content
206 Partial Content
Client Request redirected
Multiple choices
Permanently moved
302 Moved temporarily
303 and other
304 Not Modified
305 Use Proxy
Client Request Errors
Bad Request
401 Authorization Required
402 Payment Required (not used yet)
403 Forbidden
404 Not Found
405 Method Not allowed
406 Not acceptable (encoding)
407 Proxy Authentication Required
408 Request Timed out
409 Conflicting Request
410 Gone
411 Content Length Required
412 Precondition Failed
413 Request Entity Too Long
414 Request URI Too Long
415 Unsupported Media Type
Server Errors
Internal Server Error
501 Not implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version not supported

3. Password Protection Password protection
Official document:authentication, Authorization and Access control
Suppose the password file is. htpasswd
AUTHUSERFILE/USR/LOCAL/SAFEDIR/.HTPASSWD (full path name must be used here)
AuthName Enterpassword
AuthType Basic
Two common ways of validating:
Require User Windix
(Only allow user Windix login)
Require Valid-user
(All legitimate users can log in)
TIP: How to generate a password file
Using the HTPASSWD command (Apache with the self)
First build requires a password file to be created
Htpasswd-c. htpasswd User1
Add new users later
htpasswd. htpasswd User2

4. Enabling SSI via htaccess allows SSI (Server Side including) functionality via htaccess
AddType text/html. shtml
AddHandler server-parsed. shtml
Options Indexes followsymlinks Includes
DirectoryIndex index.shtml index.html

5. Blocking users by IP block user access based on IP
Order Allow,deny
Deny from 123.45.6.7
Deny from 12.34.5. (Whole C-class address)
Allow from all

6. Blocking Users/sites by referrer block user/site access based on referrer
Need mod_rewrite Module
Example 1. Block single referrer:badsite.com
Rewriteengine on
# Options +followsymlinks
Rewritecond%{http_referer} badsite.com [NC]
Rewriterule. *-[F]
Example 2. Block multiple referrer:badsite1.com, badsite2.com
Rewriteengine on
# Options +followsymlinks
Rewritecond%{http_referer} badsite1.com [Nc,or]
Rewritecond%{http_referer} badsite2.com
Rewriterule. *-[F]
[NC]-case insensitive (case-insensite)
[F]-403 Forbidden
Note that the above code comments out the "Options +followsymlinks" statement. If the server does not set followsymlinks in the httpd.conf paragraph, you need to add this sentence, otherwise you will get a "Internal Server error" error.

7. Blocking bad Bots and site Rippers (aka offline browsers) to prevent broken reptiles and offline browsers
Need mod_rewrite Module
A bad reptile? For example, some of the spiders grab the spam email address and do not follow the robots.txt crawler (such as Baidu?)
They can be judged by http_user_agent.
(But there are more shameless such as "search zhongsou.com" the flow of their own agent set as "mozilla/4.0" (compatible; MSIE 5.5; Windows NT 5.0) "Too rogue, there is nothing to do."
Rewriteengine on
Rewritecond%{http_user_agent} ^blackwidow [OR]
Rewritecond%{http_user_agent} ^bot mailto:craftbot@yahoo.com [OR]
Rewritecond%{http_user_agent} ^chinaclaw [OR]
Rewritecond%{http_user_agent} ^custo [OR]
Rewritecond%{http_user_agent} ^disco [OR]
Rewritecond%{http_user_agent} ^download Demon [OR]
Rewritecond%{http_user_agent} ^ecatch [OR]
Rewritecond%{http_user_agent} ^eirgrabber [OR]
Rewritecond%{http_user_agent} ^emailsiphon [OR]
Rewritecond%{http_user_agent} ^emailwolf [OR]
Rewritecond%{http_user_agent} ^express webpictures [OR]
Rewritecond%{http_user_agent} ^extractorpro [OR]
Rewritecond%{http_user_agent} ^eyenetie [OR]
Rewritecond%{http_user_agent} ^flashget [OR]
Rewritecond%{http_user_agent} ^getright [OR]
Rewritecond%{http_user_agent} ^getweb! [OR]
Rewritecond%{http_user_agent} ^go! Zilla [OR]
Rewritecond%{http_user_agent} ^go-ahead-got-it [OR]
Rewritecond%{http_user_agent} ^grabnet [OR]
Rewritecond%{http_user_agent} ^grafula [OR]
Rewritecond%{http_user_agent} ^hmview [OR]
Rewritecond%{http_user_agent} httrack [Nc,or]
Rewritecond%{http_user_agent} ^image stripper [OR]
Rewritecond%{http_user_agent} ^image sucker [OR]
Rewritecond%{http_user_agent} Indy Library [Nc,or]
Rewritecond%{http_user_agent} ^interget [OR]
Rewritecond%{http_user_agent} ^internet Ninja [OR]
Rewritecond%{http_user_agent} ^jetcar [OR]
Rewritecond%{http_user_agent} ^joc Web Spider [OR]
Rewritecond%{http_user_agent} ^larbin [OR]
Rewritecond%{http_user_agent} ^leechftp [OR]
Rewritecond%{http_user_agent} ^mass Downloader [OR]
Rewritecond%{http_user_agent} ^midown tool [OR]
Rewritecond%{http_user_agent} ^mister PiX [OR]
Rewritecond%{http_user_agent} ^navroad [OR]
Rewritecond%{http_user_agent} ^nearsite [OR]
Rewritecond%{http_user_agent} ^netants [OR]
Rewritecond%{http_user_agent} ^netspider [OR]
Rewritecond%{http_user_agent} ^net Vampire [OR]
Rewritecond%{http_user_agent} ^netzip [OR]
Rewritecond%{http_user_agent} ^octopus [OR]
Rewritecond%{http_user_agent} ^offline Explorer [OR]
Rewritecond%{http_user_agent} ^offline Navigator [OR]
Rewritecond%{http_user_agent} ^pagegrabber [OR]
Rewritecond%{http_user_agent} ^papa foto [OR]
Rewritecond%{http_user_agent} ^pavuk [OR]
Rewritecond%{http_user_agent} ^pcbrowser [OR]
Rewritecond%{http_user_agent} ^realdownload [OR]
Rewritecond%{http_user_agent} ^reget [OR]
Rewritecond%{http_user_agent} ^sitesnagger [OR]
Rewritecond%{http_user_agent} ^smartdownload [OR]
Rewritecond%{http_user_agent} ^superbot [OR]
Rewritecond%{http_user_agent} ^superhttp [OR]
Rewritecond%{http_user_agent} ^surfbot [OR]
Rewritecond%{http_user_agent} ^takeout [OR]
Rewritecond%{http_user_agent} ^teleport Pro [OR]
Rewritecond%{http_user_agent} ^voideye [OR]
Rewritecond%{http_user_agent} ^web Image Collector [OR]
Rewritecond%{http_user_agent} ^web sucker [OR]
Rewritecond%{http_user_agent} ^webauto [OR]
Rewritecond%{http_user_agent} ^webcopier [OR]
Rewritecond%{http_user_agent} ^webfetch [OR]
Rewritecond%{http_user_agent} ^webgo is [OR]
Rewritecond%{http_user_agent} ^webleacher [OR]
Rewritecond%{http_user_agent} ^webreaper [OR]
Rewritecond%{http_user_agent} ^websauger [OR]
Rewritecond%{http_user_agent} ^website extractor [OR]
Rewritecond%{http_user_agent} ^website quester [OR]
Rewritecond%{http_user_agent} ^webstripper [OR]
Rewritecond%{http_user_agent} ^webwhacker [OR]
Rewritecond%{http_user_agent} ^webzip [OR]
Rewritecond%{http_user_agent} ^wget [OR]
Rewritecond%{http_user_agent} ^widow [OR]
Rewritecond%{http_user_agent} ^wwwoffle [OR]
Rewritecond%{http_user_agent} ^xaldon webspider [OR]
Rewritecond%{http_user_agent} ^zeus
Rewriterule ^.*-[f,l]
[F]-403 Forbidden
[L]-?

8. Change your default directory page
DirectoryIndex index.html index.php index.cgi index.pl

9. Redirects steering
Single File
Redirect/old_dir/old_file.html http://yoursite.com/new_dir/new_file.html
Entire Directory
Redirect/old_dir Http://yoursite.com/new_dir
Effects: As if the directory was moved to the same location
[Url]http://yoursite.com/old_dir-> Http://yoursite.com/new_dir[/url]
[Url]http://yoursite.com/old_dir/dir1/test.html-> Http://yoursite.com/new_dir/dir1/test.html[/url]
TIP: A workaround for redirect when working with user directories
When you use the Apache Default User directory, such as Http://mysite.com/~windix, when you want to turn to Http://mysite.com/~windix/jump, you will find that the following redirect does not work:
Redirect/jump http://www.google.com
The correct way is to change
Redirect/~windix/jump http://www.google.com
(Source:. htaccess Redirect in "Sites" not redirecting:why?)
)

Prevent viewing of. htaccess file prevents. htaccess files from being viewed
Order Allow,deny
Deny from all

Adding MIME Types add MIME type
AddType Application/x-shockwave-flash swf
Tips: Set type for Application/octet-stream will be prompted to download

Preventing hot linking the images and other file types Anti-Theft chain
requires mod_rewrite module
Rewriteengine on
Rewritecond %{http_referer}!^$
Rewritecond%{http_referer}!^http://(www/.)? mydomain.com/.*$ [NC]
Rewriterule. ( GIF|JPG|JS|CSS) $-[F]
Parse:
If Http_referer non-empty (source for other site, not directly connected) and
if Http_referer not (www.) mydomain.com Start (Ignore case [NC]) (source non-local)
Give 403 Forbidden Error [F]
to all files that contain a. Gif/.jpg/.js/.css end can also specify a response, as shown in the following example: Replace Picture
Rewriterule. (gif|jpg) $
[r,l]
[R]-Steering (Redirect)
[L]-Connection (link)

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.