将旧域重定向到新域除robots.txt之外

时间:2014-08-04 18:20:54

标签: .htaccess redirect http-status-code-301 url-redirection

我正在将旧网站域移动到新域。我将所有页面重定向到新域上的相应页面。旧域的robots.txt文件将重定向到新域的robots.txt文件。我想重定向除robots.txt文件以外的所有页面。我该怎么做呢?

我的.htaccess文件

# Redirect the site 
RewriteEngine On 

# Take care of www.olddomain.com
 RewriteCond %{HTTP_HOST} ^www.olddomain.com$ [NC] 
 RewriteRule ^/robots\.txt$ /robots.real [L]
 RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301] 
# Takes care of olddomain.com 
 RewriteCond %{HTTP_HOST} ^olddomain.com$ [NC] 
 RewriteRule ^/robots\.txt$ /robots.real [L]
 RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]

1 个答案:

答案 0 :(得分:0)

所以,我终于明白了。

第6行和第10行需要看起来像这样:

RewriteCond %{REQUEST_URI} !^/robots.txt$ [NC]

这导致此代码:

# Redirect the site 
RewriteEngine On 

# Take care of www.olddomain.com
 RewriteCond %{HTTP_HOST} ^www.olddomain.com$ [NC] 
 RewriteCond %{REQUEST_URI} !^/robots.txt$ [NC]
 RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301] 
# Takes care of olddomain.com 
 RewriteCond %{HTTP_HOST} ^olddomain.com$ [NC] 
 RewriteCond %{REQUEST_URI} !^/robots.txt$ [NC]
 RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]