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; } } |