Although the previous article did not, but I must at least ensure that the original domain name can continue to visit the new blog, browse to new articles, and then think of using htaccess to do redirects. This time to write a record of htaccess, so as to avoid the future to be everywhere.
Here are a few of the redirect rules I used this time.
Copy Code code as follows:
Rewriteengine on
#silentash全局切换
Rewritecond%{http_host} ^ (www\.)? silentash.com$ [NC]
Rewriterule ^ (. *) $ http://jb51.net/$1 [R=301,NC]
#blog对应切换
Rewritecond%{http_host} ^blog.silentash.com$ [NC]
Rewriterule ^ (. *) $ http://jb51.net/blog [R=301,NC]
#app对应切换
Rewritecond%{http_host} ^app.silentash.com$ [NC]
Rewriterule ^ (. *) $ http://jb51.net/app/$1 [R=301,NC]
#lab对应切换
Rewritecond%{http_host} ^lab.silentash.com$ [NC]
Rewriterule ^ (. *) $ http://jb51.net/lab/$1 [R=301,NC]
#photo对应切换
Rewritecond%{http_host} ^photo.silentash.com$ [NC]
Rewriterule ^ (. *) $ http://jb51.net/photo/$1 [R=301,NC]
#只访问主域的先切换到博客
Rewritecond%{http_host} ^ (www.)? jb51.net$ [NC]
Rewritecond%{request_uri} ^ (\/)? $ [NC]
Rewriterule ^ (. *) $ http://jb51.net/blog [R=301,NC]
The following is a brief explanation of the above meaning:
"Rewriteengine on" means that the rewrite engine is open, off, and off, of course, it can't be forwarded.
"Rewritecond%{http_host} ^ (www\.)? silentash.com$ [NC]"
This is an overriding condition, preceded by%{http_host} that represents the currently visited URL, just the prefix part, the format is www.silentash.com excluding "http://" and "/", ^ represents the beginning of the string, the $ represents the end of the string, \. Represents the escaped. , if you do not escape also line, recommended escape, to prevent some servers do not support,? Represents the front bracket www\. 0 or 1 times, this rule means that if the access URL is silentash.com or www.silentash.com execute the following statement, do not meet the skip.
"Rewriterule ^ (. *) $ http://jb51.net/$1 [R=301,NC]"
This is done according to the previous conditions of the corresponding rules, where the word is to redirect to the jb51.net domain corresponding address, such as the source address is http://www.silentash.com/abc/1.html, the former part of the ^ (. *) $ will match the current request URL , but is this a match for the entire http://www.silentash.com/abc/1.html, or is it just a match for the/abc/1.html or the back of the backslash, or just the abc/1.html?
The answer is: according to the rewritebase rule, if Rewritebase is/, will match the abc/1.html, the default should be abc/1.html. The following is a regular matching value, after the combination, the previous URL will be redirected to http://jb51.net/abc/1.html, to achieve the purpose of the domain name conversion.
The following lines are similar.
Another problem is that there is no guarantee that everyone entered the URL is lowercase, if the input of uppercase, Linux system is case-sensitive, so you should add [NC] ignore case after Rewritecond.
Here are the simple grammar rules and flags:
"Rewritecond Syntax:"
Rewritecond teststring Condpattern [flags]
Other uses of Rewritecond:
'-d ' (directory)
Treat TestString as a pathname and test whether it is a directory that exists.
'-f ' (regular file)
Treat teststring as a path name and test whether it is a regular file that exists.
'-S ' (non-empty regular files)
Treat TestString as a pathname and test whether it is a regular file that exists with a size greater than 0.
'-l ' (symbolic Connection)
Treat TestString as a pathname and test whether it is a symbolic connection that exists.
'-X ' (executable)
Treat TestString as a pathname and test whether it is an existing file that has executable permissions. This permission is detected by the operating system.
'-F ' (file exists for the child request)
Check that teststring is a valid file and can be accessed under the server's current access control configuration. It uses an internal child request to check, because it will degrade the performance of the server, so please use caution!
'-u ' (URL that exists for the child request)
Check that the teststring is a valid URL and can be accessed under the server's current access control configuration. It uses an internal child request to check, because it will degrade the performance of the server, so please use caution!
"Rewriterule Syntax:"
Rewriterule pattern substitution [flags]
"Flags":
' Chain| C ' (link next rule)
This tag causes the current rule to be linked to the next rule. It has the effect that if a rule is matched, it continues to process its successor rule, that is, the tag does not work, and if the rule is not matched, its successor rule is skipped. For example, when performing an external redirect in a directory-level rule, you may need to delete ". www" (". www" should not appear here).
' cookie|co=name:val:domain[:lifetime[:p Ath]] ' (set cookies)
Set a cookie on the client. The cookie is named name, and the value is Val. Domain is the field of the cookie, such as '. Apache.org ', where the optional lifetime is the validity period of the cookie (minutes), and the optional path is the cookie's.
' Env| E=var:val ' (Set environment variable)
This tag val,val the value of the environment variable VAR to include an extensible regular Expression reverse reference ($N and%n). This tag can be used more than once to set multiple variables. These variables can be referenced indirectly in many subsequent cases, usually in Xssi (<!– #echo var= "var" –>) or CGI ($ENV {' var '}). It can also be referenced by%{env:var} in the Condpattern parameters of subsequent rewritecond directives. Use it to remember the information stripped from the URL.
' Forbidden| F ' (Enforce prohibit URL)
Forces the current URL to be blocked, that is, an HTTP response code 403 (prohibited) immediately. With this tag, you can link several rewriteconds to conditionally block certain URLs.
' Gone| G ' (Mandatory discard URL)
Forces the current URL to be deprecated, which is immediately feedback an HTTP response code 410 (deprecated). Using this tag, you can indicate that the page has been discarded and no longer exists.
' Handler| H=content-handler ' (Force specify content processor)
Changzi the content processor for the target file is Content-handler. For example, the Scriptalias directive used to simulate the Mod_alias module to force all files within the mapping folder to be handled by the "Cgi-script" processor.
' Last| L ' (end rule)
Stops the rewrite immediately and no longer applies other overriding rules. It corresponds to the last command in Perl or the break command in the C language. This tag is used to block the currently overridden URL from being rewritten again by a successor rule. For example, use it to override the URL ('/') of the root path for the actual URL (for example, '/e/www/').
' Next| N ' (Start over)
Re-perform the rewrite operation (starting from the first rule). The URL that is processed again at this time is not the original URL, but the URL that was processed by the last overriding rule. It corresponds to the next command in Perl or the Continue command in the C language. This tag can start the rewrite operation again (immediately to the beginning of the loop). But be careful not to make a dead loop!
' Nocase| NC ' (Ignore case)
It causes pattern to ignore the case, that is, when pattern matches the current URL, there is no difference between ' A-Z ' and ' A-Z '.
' Noescape| NE ' (the URI is not escaped in the output)
This tag prevents mod_rewrite from applying a regular URI escape rule to the overridden results. In general, special characters ('% ', ' $ ', '; ') etc.) will be escaped to the equivalent hexadecimal encoding ('%25′, '%24′, '%3b ', etc.). This tag can block such escapes to allow symbols such as hundred semicolons to appear in the output, such as:
rewriterule/foo/(. *)/bar?arg=p1\%3d$1 [R,ne]
Can make '/foo/zed turn to a secure request '/bar?arg=p1=zed '.
' Nosubreq| NS ' (No processing of internal child requests)
This tag forces the override engine to skip the overriding rule when the current request is an internal child request. For example, when Mod_include tries to search for a directory default file (INDEX.XXX), Apache produces a child request internally. Overriding a rule is not necessarily useful for a child request, and it may even raise an error if the entire rule set works. Therefore, you can use this tag to exclude certain rules.
Usage principles: If you add a CGI script prefix to a URL to force them to be processed by a CGI script, but the error rate (or resource overhead) of the child request processing is high, you can use this tag in this case.
' Proxy| P ' (Force for agent)
This tag causes the replacement component to be internally forced to send as a proxy request and immediately interrupt the rewrite processing, and then hand the processing over to the Mod_proxy module. You must make sure that this replacement string is a valid URI that can be processed by mod_proxy (for example, beginning with http://hostname), otherwise you will get an error returned by a proxy module. With this tag, some remote components can be mapped to the local server domain namespace, thereby enhancing the functionality of the proxypass instruction.
Note: To use this feature, the Mod_proxy module must already be enabled.
' Passthrough| PT ' (handed over to the next processor)
This tag forces the rewrite engine to set the URI field in the internal REQUEST_REC structure to the value of the FileName field, which makes the output of the rewriterule instruction able to be (converted from a URI to a filename) Alias, Scriptalias, Redirect, etc. instructions for subsequent processing [original: This flag be just a hack to enable post-processing of the output of rewriterule directives, using Ali As, Scriptalias, Redirect, and directives from various uri-to-filename translators. Give an example of what it means: if you want to rewrite/abc as/def, and then use Mod_alias to convert/def to/ghi, you can do this:
Rewriterule ^/abc (. *)/def$1 [PT]
Alias/def/ghi
If the PT mark is omitted, though it will uri=/abc/... Rewrite for filename=/def/... Part works, but subsequent mod_alias will be invalidated when attempting to convert the URI to the filename.
Note: If you need to mix multiple modules that convert URIs to file names, you must use this tag ... Mixing Mod_alias and mod_rewrite here is a classic example.
' Qsappend| QSA ' (append query string)
This tag forces the override engine to append a query string to an existing replacement string instead of a simple substitution. You can use this tag if you need to add information to the request string by overriding the rule.
' Redirect| R [=code] ' (Force redirection)
If substitution starts with http://thishost[:thisport]/(making the new URL a URI), you can enforce an external redirect. If code is not specified, an HTTP response code 302 (temporary move) is generated. If you need to use another response code in the range 300-400, just specify it here (or use one of the following symbol names: Temp (default), Permanent, seeother). It can be used to feed normalized URLs to the client, such as "/~" rewritten as "/u/", or always/u/user with slashes, and so on.
Note: When you use this tag, you must make sure that the replacement field is a valid URL. Otherwise, it will point to an invalid location! And keep in mind that this tag itself is just a http://thishost[:thisport]/prefix to the URL, and the override operation will still continue. Typically, you will also want to stop the rewrite and redirect immediately, and then use the ' L ' tag.
' Skip| S=num ' (skip successor rule)
This tag forces the rewrite engine to skip the NUM rule after the current match rule. It can simulate IF-THEN-ELSE structure: the last rule is then clause, and the skip=n rule that is skipped is else clause. Note: it and ' chain| C ' Mark is different!
' Type| T=mime-type ' (Force MIME type)
Forces the MIME type of the target file to be mime-type, which can be used to enforce a content type based on certain criteria. For example, the following instruction allows the. php file to be displayed by mod_php according to the MIME type (application/x-httpd-php-source) of the PHP source code in the case of a. phps extension call:
Rewriterule ^ (. +\.php) s$ $ [T=application/x-httpd-php-source]