Analysis of the htaccess.

Source: Internet
Author: User
1. htaccess File Use premise

The main role of htaccess is to implement URL rewriting, that is, when the browser access to a server folder through the URL, as the owner, we can receive the URL, how to receive it, is the role of this document. All accesses are implemented through URLs, so. htaccess's role is serious. Because of this, so the general site through the settings. htaccess, through a very friendly URL to attract users to come in, and then use the. htaccess to bring the user to the location that needs to be accessed.

To use this powerful feature, you have to open the rewrite module in Apache.

The previous article has talked about Windows and Ubuntu open rewrite modules using. htaccess.

In fact, the general steps to open the module are the same, whether it is Windows and Linux. 2. Introduction of htaccess Basic grammar

Open rewrite engine: Rewriteengine on

Set the root of the override: Rewritebase/-Description: Because this folder is defined, the corresponding substitution has a reference.

Match all eligible requests: Rewritecond-Description: Rewritecond defines a set of rule conditions, which can have one or more, only after the user takes the URL to meet these criteria, Our. htaccess only began to receive, otherwise the user directly to access the desired directory.

For example, in order to allow search engines to more crawl our web page and avoid duplication, we usually redirect the domain name without www to www.XXX.com, the following implementation of this function:

Rewriteengine on

Rewritecond%{http_host} ^nbphp\.com$ [NC]

Rewriterule ^ (. *) $ http://www.nbphp.com/$1 [r=301,l]

The above example redirects the nbphp.com to the www.nbphp.com

%{http_host} refers to the primary domain name of the URL that gets the user's access and then a regular expression match after the space, and the consciousness is whether it is nbphp.com.

If the user accesses the URL used to meet all the listed rewritecond conditions, the next step rewriterule start to boot, which begins to realize the important function of the. htaccess file.

Similarly, a regular expression preceded by a user parsing a user's URL other than the primary domain name nbphp.com, ^ (. *) $ means all content. Then the space is followed by the directory where we guide the user, and we take him to the new domain. Refers to what is obtained from the matching URL in the preceding brackets.

This is a complete and small example. about how to call a part of the URL in Rewritecond, we can refer to this article (Apache mod_rewrite Learning (Rewritecond rewrite rules), 3, now learn to use, learning regular expressions.

Recommend a classic tutorial: Regular expression 30-minute introductory tutorial

This tutorial is really very simple, after reading basically write some simple positive then there is no problem. It's a tool that needs to be used for a long time, and it doesn't have to be forgotten at intervals, so I read this tutorial every time. In fact, after learning the important thing is a little content. I simply listed the following:

. All characters except for line breaks

\w matches letters or numbers or underscores or Chinese characters

\s matches any whitespace character

\d Matching numbers

\b Match the start or end of a word

^ Start of Match string

$ end of Match string

* Repeat 0 or more times

* Repeat 0 or more times

+ Repeat one or more times

? Repeat 0 times or once

{n} repeat n times

{N,} repeat n or more times

{N,m} repeats n to M times

When the substitution is applied, the matching content in the first () is followed by a reference, and the match in the second () is applied in $ ...

Recommend a practical, regular online test site http://www.regextester.com/

Let's analyze the rewrite in discuz7.0 search engine optimization htaccess. Rewriterule ^forum-([0-9]+)-([0-9]+) \.html$ forumdisplay.php?fid=$1&page=$2

First join the user through the Nbphp.com/forum-2-3.html Access Discuz Forum, Then first pass. htaccess filter to see if it is needed. htaccess to guide the user, if the list of Rewritecond to meet the conditions then rewrite, Discuz is not listed Rewritecond so should all be rewritten. So start the transliteration, forum-2-3.html this exactly matches the list of ^forum-([0-9]+)-([0-9]+) \.html$ regular expressions. and $2, $3, so take him back, that is, forumdisplay.php?fid=2&page=3 plus the previous rewritebase specified file directory, then bring him to the directory forumdisplay.php?fid=2 &page=3. 4, Common. htaccess application examples (some examples from four examples of actual combat explanation. htaccess file rewrite rules) 4.1 Prevent hotlinking, if come to access the URL of the end of JPE jpg bmp png The user is not from our site, so let him see a picture of our site.

Rewriteengine on

Rewritecond%{http_referer}!^http://(. +.)? mysite.com/[NC]

Rewritecond%{http_referer}!^$

Rewriterule. *. (jpe?g|gif|bmp|png) $/images/nohotlink.jpg [L] 4.2 site upgrade, only specific IP access, other users will see an upgrade page

Rewriteengine on

Rewritecond%{request_uri}!/upgrade.html$

Rewritecond%{remote_host}!^24\.121\.202\.30

Rewriterule $ http://www.nbphp.com/upgrade.html [r=302,l] 4.3 turn old domain name to new domain name

# REDIRECT from old domain to new domain

Rewriteengine on

Rewriterule ^ (. *) $http://www.yourdomain.com/$1[r=301,l] 5, some other function 5.1 directory that leads to the wrong document

ErrorDocument 400/errors/badrequest.html

ErrorDocument 404 http://yoursite/errors/notfound.html

ErrorDocument 401 "Authorization Required 5.2 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 5.3 prevents directory browsing

# Disable Directory browsing

Options all-indexes 5.4 Set default home

# Serve alternate default index page

DirectoryIndex about.html 5.5 take some old links to the new links-search engine optimization seo

Redirect 301/d/file.htmlhttp://www.htaccesselite.com/r/file.html 5.6 Sets up e-mail for the server administrator.

Serversignature EMail

Setenv SERVER_ADMINdefault@domain.com

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.