Linux server modify .htaccess file to achieve 301 redirect

  

For the purpose of SEO, PR value transfer, URL conversion, we need to use 301 redirect when the website is first built and website migration, usually including domain name to domain name, directory A redirect to a directory and a separate URL to another separate URL.

For 301 redirection on a virtual host, there are two most common methods: the first one is to use cPanel settings. Using cPanel is fairly straightforward. Log in to your cPanel–>Domain–>Redirects and select the appropriate options to complete the setup, which is not discussed here. The second is to edit .htaccess directly. The essence of both is to modify the file .htaccess, only the former is manually edited, the latter is done by cPanel. The second method introduced here is mainly.

Note: Be sure to back up the .htaccess file in the appropriate directory before setting the 301 redirect.

1. Redirect domain.com to www.domain.com

This kind of redirection is designed to make the domain name unique, which is what the website SEO must do, and then redirect www.domain. Com to domain.com is also for the same reason, but in a different form. Open the .htaccess file and add the following rules. (The following rules are for the primary domain name, the subdomain should be modified, otherwise the subdomain will be rewritten. The following will explain this specifically.)

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

2. Redirect www.domain. Com to domain.com

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

3. Redirect olddomain.com to www.newdomain.com

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

4. Redirect olddomain.com to newdomain.com

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !olddomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com$1 [L,R=301]

5. Redirect domain.com/file/file.php to otherdomain.com/otherfile/other.php

RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^file/File.php$ http://www.otherdomain.com/otherfile /other.php [R=301,L]

5. Redirecting domain.com to www.domain.com without affecting subdomains

Special instructions do not affect subdomains Because the method mentioned above is only feasible without the subdomain name. If the domain name contains a subdomain, and the subdomain does not want to be redirected, then the following method is needed to perform the 301 redirect;

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www.45it.com| T.webjx.com)$[NC]
RewriteRule ^(.*)$ http://www.45it.com$1 [L,R=301]

The above code description: first The line indicates that mod-rewrite is enabled, and the second line indicates the object to be redirected. The code means all the domain names except the two domains www.45it.com and t.webjx.com, because this blog contains The meager t.webjx.com of a subdomain means that when the domain name is t.webjx.com, no redirection is performed, and the third line indicates the target to be redirected. The above is an example, in fact, the main idea is still a regular expression, if there are multiple subdomains do not want to redirect, the same can be added in the second line! Any questions can be raised later, I know as much as possible to answer.

Special note is that the methods in this article are only applicable to systems that support htaccess files, that is, only for Linux systems. As for the space of the windows system, I have not studied it yet.

Copyright © Windows knowledge All Rights Reserved