Apache 301 redirection and redirection rules

Source: Internet
Author: User
Tags to domain

The apache host uses the htaccess file to implement 301 Redirection. First, check the following code:

The code is as follows: Copy code

RewriteEngine on
RewriteCond % {HTTP_HOST} ^ (aaa.org) (: 80 )? [NC]
RewriteRule ^ (. *) http://www.aaa.com/#1 [R = 301, L]

For R = 301, L is the redirection parameter. Here, R can write numbers or words, so R = permanent can also be written to indicate permanent redirection. L indicates that the rewrite operation is stopped.

Permanent returns a permanent redirect status code (301), indicating that the location change of the resource is permanent.

We may see another way of writing during redirection.

The code is as follows: Copy code

Redirect 301/old.html http://www.xxxx.org/new.html
Redirect permanent/one http://www.xxxx.org/two
RedirectMatch 301 (. * pai.gif $ yun_qi_img/ww2.xxxx.org

Next we will introduce the Linux/unix server, which is limited to 301 of the two types of servers.

Redirect domain name 301 without www to domain name with www
Create a. htaccess file and add the following code:

The code is as follows: Copy code

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

Apache 301 redirects to a single url

The code is as follows: Copy code

RewriteEngine On
Redirect permanent/old-directory/old-file.html http://www.url.com/new-directory/new-file.html


Php 301 redirection

The code is as follows: Copy code

<?
Header ("HTTP/1.1 301 Moved Permanently ");
Header ("Location: http://www.url.com ");
?>


Nginx 301 redirection


Add the following filter rules and rewrite rules to the Nginx configuration file:
First case: Access Site A and direct it to site B

The code is as follows: Copy code

Server {
Server_name www.A.com;
Rewrite ^ (. *) http://www. B .com $1 permanent;
}


Case 2: not redirect all accesses to the specified page

The code is as follows: Copy code

Server {
Server_name www.A.com;
If ($ host! = 'A. Com '){
Rewrite ^/(. *) $ http://www. B .com/#1 permanent;
}
}

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.