1. htaccess File Usage Prerequisites
The main function of htaccess is to implement URL rewriting, that is, when the browser through the URL access to a server folder, as the owner, we can receive this URL, specifically how to receive it, is the role of this file. All accesses are implemented via URLs, so. The role of the. Htaccess is trivial. Because of this, so the general site by setting. htaccess, which attracts users through a very friendly URL, then uses. htaccess to take the user to a location that needs to be accessed.
To use this powerful feature, you have to open the rewrite module inside 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, both Windows and Linux.
2. Introduction to basic grammar of htaccess
Turn on rewrite engine: Rewriteengine on
To set the root of an 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 if the URL that the user comes to meets these conditions. Our. htaccess only start receiving, otherwise the user will go directly to access the required directory.
For example, in order to allow search engines to crawl more of our pages and avoid repeated scratching, we usually redirect the domain name without www to www.XXX.com, which realizes this function as follows:
Rewriteengine on
Rewritecond%{http_host} ^nbphp\.com$ [NC]
Rewriterule ^ (. *) $ http://www.nbphp.com/$1 [r=301,l]
The previous example redirects nbphp.com to www.nbphp.com
%{http_host} refers to the primary domain name of the URL that gets the user's access and then the space is followed by a regular expression match, and the consciousness is whether it is nbphp.com.
If a user accesses a URL that satisfies all of the listed rewritecond conditions, then the next rewriterule starts with booting, which starts to implement the important functionality of the. htaccess file.
Similarly, the preceding is the regular expression, the user analyzes the user's except the primary domain name nbphp.com the URL, ^ (. *) $ means all content. Then the space is written in the directory we guide the user to visit, we take him to a new domain name. $ = refers to the contents of the preceding brackets that match the URL.
This is a complete and small example. We can refer to this article about how to invoke a part of the URL in Rewritecond (Apache mod_rewrite Learning (Rewritecond rewrite rules);
3, now learn to use, learning regular expression.
Recommend a classic tutorial: Regular expression 30-minute introductory tutorial
This tutorial is really simple, and after reading basically write some simple regular there is no problem. Regular is a long-term use of the tool, not to forget the time interval, 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 line break
\w match letters or numbers or underscores or kanji
\s matches any whitespace character
\d Matching numbers
\b Match the beginning or end of a word
^ Start of matching string
$ match End of string
* Repeat 0 or more times
* Repeat 0 or more times
+ Repeat one or more times
? Repeat 0 or one time
{n} repeats n times
{n,} repeats n or more times
{N,m} repeats n to M times
When the substitution is applied, the match in the first () is followed by a reference, and the match in the second () is applied with the app ...
Recommend a useful regular online testing site http://www.regextester.com/
Let's analyze the rewrite of discuz7.0 search engine optimization htaccess.
Rewriterule ^forum-([0-9]+)-([0-9]+) \.html$ forumdisplay.php?fid=$1&page=$2
First join the user through nbphp.com/forum-2-3.html to visit the Discuz forum, Then first pass the. htaccess filter to see if it is needed. htaccess Guide the user, if the list of Rewritecond conditions are satisfied then rewrite, Discuz is not listed Rewritecond so should be all rewritten. So start the transliteration, forum-2-3.html this exactly matches the listed ^forum-([0-9]+)-([0-9]+) \.html$ regular expression. and $2, $3, so take him to the back, that is, forumdisplay.php?fid=2&page=3 plus the file directory specified in the previous rewritebase, then bring him to the directory forumdisplay.php?fid=2 &page=3.
4, Common. htaccess application examples (some examples are from four examples of actual combat explanation. htaccess File rewrite rules) 4.1 Prevent hotlinking, if come to access jpe jpg bmp PNG End URL user not from our site, Then let him see a picture of our site's display.
Rewriteengine on
Rewritecond%{http_referer}!^http://(. +.)? mysite.com/[NC]
Rewritecond%{http_referer}!^$
Rewriterule. *. (jpe?g|gif|bmp|png) $/images/nohotlink.jpg [L]
4.2 When the website is upgraded, only certain IP can be accessed, and 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 the old domain name to the new domain
# REDIRECT from old domain to new domain
Rewriteengine on
Rewriterule ^ (. *) $http://www.yourdomain.com/$1[r=301,l]
5. Some other functions 5.1 lead to the directory of the wrong document
ErrorDocument 400/errors/badrequest.html
ErrorDocument 404 http://yoursite/errors/notfound.html
ErrorDocument 401 "Authorization Required
5.2 Blocking Users by IP block user access based on IP
Order Allow,deny
Deny from 123.45.6.7
Deny from 12.34.5. (entire class C address)
Allow from all
5.3 Preventing Directory Browsing
# Disable Directory browsing
Options all-indexes
5.4 Set default home page
# Serve alternate default index page
DirectoryIndex about.html
5.5 Take some old links to the new link-search engine optimization seo
Redirect 301/d/file.htmlhttp://www.htaccesselite.com/r/file.html
5.6 Set up e-mail for the server administrator.
Serversignature EMail
SETENV [email protected]
PHP. htaccess file Usage details