Apache's Rewrite sets the method for multiple 301s to jump to the main domain

  • 2020-05-10 23:19:23
  • OfStack

If the website has multiple domains, but you don't want to spread the weight, you can turn on the Rewrite setting to jump multiple domains 301 to the main domain. The specific code is as follows:


RewriteEngine On
RewriteCond %{HTTP_HOST} ^mituxiu.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.mituxiu.com [NC,OR]
RewriteCond %{HTTP_HOST} ^metooshow.com [NC]
RewriteRule ^(.*)$ http://www.metooshow.com/$1 [L,R=301]

Among them, www.metooshow.com is the domain name that wants to appear eventually, while mituxiu.com, www.mituxiu.com and metooshow.com are all domains that want to be jumped by 301. (subdomains are also considered independent new domains when weights are calculated, so this method can also be used to jump between domains without www and domains with www.) where OR means "or", you can continue to add more domains with OR.

The configuration code can be written in httpd.conf or in the.htaccess file. If you are an wordpress user, remember to write the configuration code outside # BEGIN WordPress and # END WordPress when modifying the.htaccess file, otherwise it will be overwritten by wordpress Settings.

Question mark matching problem
Here's an example... The revision of the company's website required the inclusion of page 301 on the new address, and it was difficult to write Apache Rewrite1. Code:


/index.php/index/supply.html?cid=101000
= " 
/sell/list-101000.html

Later, I realized that the problem might be caused by the question mark. When I checked the data, I found that it was caused by the question mark. The code was as follows:


 RewriteCond %{QUERY_STRING} ^cid=(.+)$
 RewriteRule ^/index.php/index/supply\.html$ /sell/list-%1.html? [R=301,L]

Description:
RewriteRule Pattern does not process the query character after the question mark when matching, but needs an RewriteCond instruction with the variable %{QUERY_STRING}.

Questions that require an idea:
1. In the new address /sell/list-% 1.html, you need to use the % plus number to get the corresponding parameter content in the RewriteCond configuration, instead of the usual $(match the content in RewriteRule).
2. New address /sell/list-%1. You need to put a question mark at the end of the query string to end the query string, otherwise it will appear as /sell/ list-1000. html? cid=1000.

The official explanation
Pattern does not match against the query string. To do this, you must use an RewriteCond directive with the variable %{QUERY_STRING}. Of course, you can also create URL with the query string in the substitution string: use a question mark in the substitution string to indicate that the rest of the string should be re-injected into QUERY_STRING. To delete an existing request string, replace the string with a question mark. To combine old and new query strings, use the [QSA] flag.


Related articles: