.htaccess将旧域重定向到新域但排除非www版本,robots.txt和一个文件

时间:2016-05-10 18:28:08

标签: .htaccess redirect

我正在尝试将旧域重定向到新域,但保留非www主页 robots.txt 和一个文件( myfile.html )落后。 这是我想要做的事情

all www.olddomain.com > all www.newdomain.com
olddomain.com > 404 error
olddomain.com/robots.txt > olddomain.com/robots.txt
olddomain.com/myfile.html > olddomain.com/myfile.html

以下是我在下面尝试的内容:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !/(myfile\.html|robots\.txt) [NC]
RewriteRule ^$ http://olddomanin.com [R=410,L]
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

由于某种原因,它无法正常工作。

1 个答案:

答案 0 :(得分:1)

将以下代码放在根.htaccess文件中:

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !/(myfile\.html|robots\.txt) [NC]
RewriteRule ^(.*)$ /$1 [R=404,L]

# above code will capture any request not having www except myfile.html &
# robots.txt and directed to 404 error page and if you want 410 change it 

RewriteCond %{REQUEST_URI} !/(myfile\.html|robots\.txt) [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

# the above code will capture every request that passed the above rule 
# except myfile.html & robots.txt and redirected it to a new domain