3

Essentially I need an .htaccess file that will redirect all traffic to our new domain. It need to work in the following conditions:

http://www.olddomain.com/path/file.php => http://www.newdomain.com/path/file.php
https://www.olddomain.com/path/file.php => http://www.newdomain.com/path/file.php

(note in the above case the https redirect to http - this is not an issue)

Also:

http://olddomain.com/path/file.php => http://newdomain.com/path/file.php
https://olddomain.com/path/file.php => http://newdomain.com/path/file.php

I've almost got it working by first redirecting the https version of www.olddomin.com to http version of www.olddomain.com which then redirects to the http version of the new domain, the problem I have is with the non-www version of https://olddomain.com which redirects to http://olddomain.com and then stops.

The code I am using is:

RewriteEngine On
RewriteBase /

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

This almost works except that https://olddomain.com/path/file.php just redirects to http://olddomain/path/file.php and stops and doesn't get redirected to http://newdomain.com/path/file.php

Any help would be appreciated.

4

1 回答 1

4

您只需要olddomain 文件中的这条规则:DOCUMENT_ROOT/.htaccess

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ http://%1newdomain.com%{REQUEST_URI} [R=301,L,NE]

解释:

  • NC- 忽略大小写
  • L- 最后的
  • R=301- 发送301状态到浏览器
  • NE- 没有逃避
  • %1- 是我们首先(...)RewriteCond. 那将是www或空白
于 2013-10-31T10:36:58.863 回答