添加微信咨詢
通過URL重寫規則語句實現301重定向
在網站運營的過程中,有時因一些不同的訪問要求(譬如使用http://1.uwill.com.cn訪問的是http://www.zokz.cn/1,使用http://2.uwill.com.cn訪問的是http://www.zokz.cn/2),需對網站訪問進行重定向設置。最直接最有效的做法是通過URL重寫規則實現。下面給出URL重寫實現重定向的的一些常用范例。
注意:在設置301重定向之前務必備份相應目錄下的.htaccess文件。(Windows主機是在/htdocs目錄下,Linux主機是在根目錄下)
1.重定向uwill.com.cn到www.zokz.cn
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.zokz.cn$ [NC]
RewriteRule ^(.*)$ http://www.zokz.cn/$1 [L,R=301]
2.重定向www.zokz.cn到uwill.com.cn
RewriteEngine On
RewriteCond %{HTTP_HOST} !^uwill.com.cn$ [NC]
RewriteRule ^(.*)$ http://uwill.com.cn/$1 [L,R=301]
3.重定向olduwill.com.cn到www.newuwill.com.cn
RewriteEngine On
RewriteCond %{HTTP_HOST} !olduwill.com.cn$ [NC]
RewriteRule ^(.*)$ http://www.newuwill.com.cn/$1 [L,R=301]
4.重定向olduwill.com.cn 到 newuwill.com.cn
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !olduwill.com.cn$ [NC]
RewriteRule ^(.*)$ http://newuwill.com.cn/$1 [L,R=301]
5.重定向uwill.com.cn/file/file.php 到 otheruwill.com.cn/otherfile/other.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.zokz.cn$
RewriteRule ^file/file.php$ http://www.otheruwill.com.cn/otherfile/other.php [R=301,L]