Summary of HTTP access 301 redirect settings in apache

Source: Internet
Author: User
Tags php code

1. Redirect www.111cn.net to www.111cn.net

Open the. htaccess file and add the following rules.

The code is as follows: Copy code

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


2. Redirect old.com to www.111cn.net

The code is as follows: Copy code

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

To redirect a domain name without www to a domain name with www, you only need to replace the corresponding domain name with your own. If I want to jump a domain name with www to a domain name without www, I feel that it is not as simple as changing the location of two domain names, but it can be associated with the code in front of the domain name (as of % and ^) try changing the order


3. Redirect old.com/file/file.php to new.com/otherfile/other.php

The code is as follows: Copy code

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


The above code is to jump to directories and files. If your apache does not support htaccess files, we can directly set them in apache.

Use mod_rewrite to rewrite the URL
APACHE

The code is as follows: Copy code
Options + FollowSymLinks
RewriteEngine on
RewriteCond % {HTTP_HOST} ^ baidu.com
RewriteRule ^ (. *) $ http://www.baidu.com/#1 [R = permanent, L]

For more details, see: http://www.111cn.net/phper/apache/38888.htm

If you do not have the apache permission, you can implement it in php code.

The code is as follows: Copy code

@ Header ("http/1.1 404 not found ");
@ Header ("status: 404 not found ");
Include ("404.html"); // jump to a page. This method is recommended.

404.html file is an error prompt file

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.