Apache use. htaccess file Implementation 301 Redirect Common methods

Source: Internet
Author: User
Tags to domain

From a search engine optimization perspective, the current 301 redirect is the most feasible way to redirect the site directory changes. After you change the address using 301 Redirect, the search engine will only index the new address, but also the old address under the original link transferred to the new address, and these operations do not affect the site in the search engine rankings.

The most direct way to achieve 301 redirection is to edit the. htaccess file and to learn more about how to use the Htaccess file, click here to view it. The garden needs to remind you that before you operate on the. htaccess file, be sure to back up the original. htaccess file to avoid unnecessary trouble with the modification error.

1. redirect domain.com to Www.Domain.Com

This redirection is very common, the ultimate goal is to achieve the uniqueness of the domain name, but also SEO must do. The implementation method is to include the following rules in the. htaccess file:
The code is as follows:

Rewriteengine on
Rewritecond%{http_host}!^www.domain.com$ [NC]
Rewriterule ^ (. *) $ http://www.domain.com/$1 [l,r=301]

Note: After using this 301 redirect, you will automatically be directed to www.domain.com when you open a URL similar to domain.com.
2. redirect Www.Domain.Com to domain.com

This operation is exactly the same as the above domain name display is the opposite, the rules are as follows:
The code is as follows:


Rewriteengine on
Rewritecond%{http_host}!^domain.com$ [NC]
Rewriterule ^ (. *) $ http://domain.com/$1 [l,r=301]


Note: Using this 301 redirect, you will automatically be directed to domain.com when you open a URL similar to www.domain.com.
3. REDIRECT olddomain.com to newdomain.com

This operation is often used to replace the domain name, many webmasters for various reasons may want to change the domain name for the site, at this time more use the following rules to achieve redirection:
The code is as follows:

Rewriteengine on
Rewritebase/
Rewritecond%{http_host}!olddomain.com$ [NC]
Rewriterule ^ (. *) $ http://newdomain.com/$1 [l,r=301]

Note: When the user opens the old domain name, will automatically redirect to the new domain name under the site, at this time the domain name display format is not with www.
4. redirect olddomain.com to Www.Newdomain.Com

This operation is based on a third way of improvement, except that the display URL is displayed as the one with www.
The code is as follows:

Rewriteengine on
Rewritecond%{http_host}!olddomain.com$ [NC]
Rewriterule ^ (. *) $ http://www.newdomain.com/$1 [l,r=301]

Note: When the user opens the old domain name, it will automatically redirect to the site under the new domain name, and the format of the Web address display is with www.

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

This action is used when changing a domain name while the site directory path changes, as follows:
The code is as follows:

Rewritecond%{http_host} ^www.domain.com$
Rewriterule ^file/file.php$ http://www.otherdomain.com/otherfile/other.php [r=301,l]
Note: When the user accesses the old domain name path, it is redirected to the new domain name under the new path.

301 redirect Under 6.IIS server

To do this: Open Internet Information Services Manager, right-click on the page or directory you want to redirect, select Redirect to URL, enter the address of the destination page in the dialog box, and remember to select the "Permanent redirect for resources" and click "Apply".
Note: To remind you again, be sure to select "Permanent redirection of resources."

7.Apache Server Implementation 301 redirect

Methods for implementing 301 redirects on the Apache server The garden has been mentioned in previous articles, just add the following rules to the. htaccess file:
The code is as follows:


Redirect/old directory/old document name new document address
It may also be:

The code is as follows:

Redirect/olddirectory http://www.newsite.com/newdirectory

Note: Be sure to pay attention to the writing format, the first is the same domain name to direct a document to the new directory address, the second is to define the old directory to the new domain name of the new directory.

The above seven methods are often used in Web site management and optimization of the 301 redirect method, of course, you may encounter a very different situation, this time it is necessary for their own purposes to adjust the rules of the order to adapt to the various needs.

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.