The. htaccess file is no stranger to everyone. If you don't know them, search for them ...... By the way, this is a good thing and worth learning.
It seems that there are very limited tutorials on the. htaccess Programming Method on the Internet. I believe that few bloggers write their own tutorials, right?
I searched several common rules here and summarized the usage of rewrite rules. Of course, this is only a small part of the. htaccess function, but it is quite practical.
If you are familiar with the preparation of rewrite rules, you can enhance the control of website URLs, which is very beneficial to user experience and SEO.
I. Anti-leech
View Source
Print?
2 |
RewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC] |
3 |
RewriteCond %{HTTP_REFERER} !^$ |
4 |
RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L] |
A line-by-line explanation:
1.Enable the rewrite function. It is possible that the server is already enabled globally, but it is okay to write more.
2.The rewritecond command defines the effective conditions for finding addresses that match the conditions. The following content is matched by a regular expression. The meaning is that the sent request is not sent by mysite.com, that is, leeching. The [Nc] at the end indicates case-insensitive.
3.The host prefix for sending the request is not empty.
4.The rewriterule command defines the rewrite rule to rewrite the matched address according to this rule. In this example, replace all the Suffixes in the format of these images with one image. [L] indicates that this is the last rule.
In only these four lines, is anti-leech amazing (--|) and complicated to write.
Here we summarize several common parameters (not all ):
Under rewritecond:
[Nc] case-insensitive
[Or] used to connect to the next rule
Under rewriterule:
[R] force redirect. [R = Code] Code is 302 by default.
[F] disabling URLs and returning an HTTP 403 Error
[L] This is the last rule, and the subsequent content is useless.
A tutorial on regular expressions (very detailed): http://www.unibetter.com/deerchao/zhengzhe-biaodashi-jiaocheng-se.htm
Ii. url Standardization
View Source
Print?
1 |
Options +FollowSymLinks |
3 |
rewriteCond %{http_host} ^yourdomain.com [NC] |
4 |
rewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L] |
This is an example of redirecting all the second-level domain names to www.yourdomain.com. Does it seem easy now?
Note that options + followsymlinks is not mandatory, but in some servers, if followsymlinks is not set, the 500 error may occur.
Let's take a look at a fun redirection.
View Source
Print?
3 |
RewriteCond %{HTTP_USER_AGENT} (Googlebot) |
4 |
RewriteRule ^ http://abc.com/ [R=301,L] |
1.Enable the rewrite function.
2.Rewritebase command to set the baseline URL for directory-level rewriting. It can be understood that this directory (the directory where this. htaccess is located) is assumed as the base URL prefix. This is useless in this example.
3.Rewritecond command. Requests that match all user_agent values to googlebot are sent.
4.Rewriterule command. In this example, all these requests are redirected to abc.com.
In this example, this configuration should be done by hackers, pointing Google Spider to a website is equal to forging PR.
3. Temporary error page
When your website is being upgraded or modified, you 'd better let the visitor go to the specified page instead of the unfinished page or error page.
In this case, we can make a 302 jump.
View Source
Print?
2 |
RewriteCond %{REQUEST_URI} !/maintenance.html$ |
3 |
RewriteCond %{REMOTE_ADDR} !^123.123.123.123 |
4 |
RewriteRule $ /error.html [R=302,L] |
1.Enable rewrite. -|
2.Request_uri: the requested URL value. All requests for accessing the maintenance.html page.
3.Remote_addr: the IP address of the request sent to the server. In this example, set your own IP address so that only you can access it.
4.Rewriterule command. In this example, all these requests are directed to error.html.
In this example, we will summarize several common Regular Expressions and special symbols.
(. *) Is used to match all content in a region. For example, ABC/DEF/Ghi can be matched.
([A-Za-Z _] +) matches English words and can be connected with-and.
([0-9] +) matches multiple-digit characters and is usually used to match IDs.
([0-9]) matches only one digit.
^ Indicates the start of a regular expression.
$ Indicates the completion of the regular expression.
4. Redirect the RSS address to feedburner
In addition to modifying the RSS address in the template,. htaccess can also change the RSS address for greater convenience.
View Source
Print?
2 |
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC] |
3 |
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC] |
4 |
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feed.onexin.net/yourname [R=302,NC,L] |
With the above summary, this example is actually very simple.
The only thing to note is that you must ensure that the http_user_agent is correct. In fact, you do not often change the template .. It may be easier to directly modify the template.
At the end, we recommend a few things for the slots:
Online. htaccess Generator: Http://tool.onexin.net/htaccess/
Mod_rewrite module Chinese Reference Manual: Http://help.onexin.net/apache/mod/mod_rewrite.html
P.s.In fact, I think rewrite is just a module of Apache. It is enough to search and write, but it is impossible to search a rule directly. There is no need to spend too much effort to learn. However, the regular expressions are very practical and worth further study.
Reprinted please indicate the source: http://www.onexin.net/latest-real-talk-htaccess-file-rewrite-rules/