Apacherewrite redirection rules, rewrite regular expressions, second-level domain names point to first-level domain names, multiple domain names point to one domain name, IP to domain name

Source: Internet
Author: User
Tags to domain
Apacherewrite redirection rules, rewrite regular expressions, second-level domain names point to first-level domain names, multiple domain names point to one domain name, IP to domain name first there is a linux server, there are four IP addresses can point to this server, there are 10 domain names www.2cto. the com4 IP addresses are respectively 1.1.1.11.1.1.21.1...
Apache rewrite redirection rules, rewrite regular expressions, second-level domain names point to first-level domain names, multiple domain names point to one domain name, IP to domain names first there is a linux server, there are four IP addresses can point to this server, there are 10 domain names www.2cto.com, and the four IP addresses are 1.1.1.11.1.1.21.1.1.31.1.1.4. The 10 domain names are a.com a.org, and the corresponding ip address 1.1.1.1. the corresponding website directory is at/home/www/AB .com B .org/ home/www/bc.com c.org c.net corresponding ip 1.1.1.3 corresponding website directory in/home/www/cd.com e.org f.net corresponding ip 1.1.1.4 corresponding website directory in/home/www/d now want to achieve this effect when you enter the IP address 1.1.1.1 or a.com a.org, the system automatically jumps to www.a.com. when you enter the IP address 1.1.1.2 or B .com B .org, the system automatically jumps to www. B .com. when you enter the IP address 1.1.1.3 or c.com c.org c.net, the system automatically jumps to www.c.com. when you enter the IP address 1.1.1.4 or d.com e.org f.net, the system automatically jumps to www.e.org. when you enter a nonexistent second-level domain name of a.com and a.org such as aaa.a.com bbb.a.com 111.a.com 222.a.org, automatically jump to www.a.com, the level-1 standard domain name. the first method is to modify the server directory, that is, to add a directory. htaccess: The second method is the same as the first method. The only thing you need to note is to modify the directory. htaccess should also be written with RewriteEngine on, that is, the section where you modify the website directory, to prevent the website content from being displayed as a list. (If Options-Indexes FollowSymLinks is disabled, Options Indexes FollowSymLinks is enabled.) 1: enable wildcard domain name resolution for domain name management. that is, to add A record for the domain name, such as a.com, there is A record in the original A record, @ pointing to 1.1.1.1 add A record * pointing to 1.1.1.1 other domain names with wildcard domain name resolution when this second-level domain name has no content, or it is useful when it does not exist. if there is content corresponding to the second-level domain name, such as bbs.a.com forum.a.com shop.a.com mail, you need to go to the domain name management, add cname, host to write bbs, forum or shop mail, etc, direct to bbs.a.com forum.a.com shop.a.com mail.a.com and save the domain name modification for about one hour (godaddy takes effect in a few minutes .) www.2cto.com 2: Open apache http. conf file. check whether LoadModule rewrite_module modules/mod_rewrite.so is enabled. if it is not enabled, remove it. # enable it. check whether this exists in modules. if it is not necessary to re-compile this Module 3: add the following content at the end: NameVirtualHost 1.1.1.1: 80 NameVirtualHost 1.1.1.2: 80 NameVirtualHost 1.1.1.3: 80 NameVirtualHost 1.1.1.4: 80 RewriteEngine on www.2cto.com ServerAdmin rainysia@gmail.comDocumentRoot/home/www/aServerName www. a. comServerAlias * .a.com *. a. orgDirectoryIndex index.html index.htm index. php index.shtml index. shtm index. tplRewriteEngine %{ HTTP_HOST }! ^ Www.a.com [NC] RewriteCond % {HTTP_HOST }! ^ $ RewriteRule ^ (. *) http://www.a.com/[L] ServerAdmin rainysia@gmail.comDocumentRoot/home/www/bServerName www. B. comServerAlias *. B .com *. B. orgDirectoryIndex index.html index.htm index. php index.shtml index. shtm index. tplRewriteEngine failed % {HTTP_HOST }! ^ Www. B .com [NC] RewriteCond % {HTTP_HOST }! ^ $ RewriteRule ^ (. *) http://www. B .com/[L]   ServerAdmin rainysia@gmail.comDocumentRoot/home/www/cServerName www. c. comServerAlias * .c.com * .c.org *. c. netDirectoryIndex index.html index.htm index. php index.shtml index. shtm index. tplRewriteEngine %{ HTTP_HOST }! ^ Www.c.com [NC] RewriteCond % {HTTP_HOST }! ^ $ RewriteRule ^ (. *) http://www.c.com/[L] Www.2cto.com ServerAdmin rainysia@gmail.comDocumentRoot/home/www/dServerName www. e. orgServerAlias * .d.com * .e.org *. f. netDirectoryIndex index.html index.htm index. php index.shtml index. shtm index. HTTP_HOST % {HTTP_HOST }! ^ Www.e.org [NC] RewriteCond % {HTTP_HOST }! ^ $ RewriteRule ^ (. *) http://www.e.org/[L] After saving and exiting, restart/etc/init. d/httpd restart to restart apache, which can be explained by the rewrite rules of www.a.com! Does it mean that as long as RewriteCond is not through www.a.com, it will be switched to www.a.com, including access through IP address, second-level domain name, etc., and will be switched to some definitions of apache rewrite regular because of this, it fully complies with perl regular expressions, including text. represents the non-occurrence of any [^ string] character in a single [string] character string, that is, the character text1 that is not in the string | text2 or the condition is often used in the domain name suffix, such as org | com | net? The previous character appears 0 or 1 time * currently it is 0 or N times + it is once or N times the grouping (text) is used to set a selection boundary, or reference after generation, in RewriteRule, you can use $ N to reference the nth Group anchor ^ anchor to the beginning of the line $ anchor to the end of the line escape \ c to escape the given character c and other symbols escape for example ". [] () and so on. 1) R [= code] (force redirect) force external redirection to force the replacement string to be added http://thishost [: Thisport]/The URL with a prefix redirected to an external URL. if the code is not specified, the default 302 HTTP status code will be used. 2) F (force URL to be forbidden) disables the URL and returns a 403HTTP status code. 3) G (force URL to be gone) forces the URL to be GONE and returns the response HTTP status code. 4) P (force proxy) enforces proxy forwarding. 5) L (last rule) indicates that the current rule is the last rule. after the analysis is stopped, the rule is overwritten. 6) N (next round) re-runs the rewrite process from the first rule. 7) C (chained with next rule) is associated with the next rule. if the rule matches, the flag will be processed normally. if the flag does not match, all the associated rules below will be skipped. 8) T = MIME-type (force MIME type) force MIME type 9) NS (used only if no internal sub-request) is only used for 10 instead of internal sub-requests) NC (no case) case-insensitive 11) QSA (query string append) append request string 12) NE (no URI escaping of output) does not escape special characters in the output, for example: rewriteRule/foo /(. *)/bar? Arg = P1 % 3d $1 [R, NE] will be able to correctly convert/foo/zoo to/bar? Arg = P1 = zoo13) PT (pass through to next handler) passed to the next processing example: RewriteRule ^/abc (. *)/def $1 [PT] # it will be handed over to the/def rule for processing Alias/def/ghi14) S = num (skip next rule (s) to skip num rule 15) E = VAR: VAL (set environment variable) sets the environment variable by rainysia
Related Article

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.