.htaccess将OldDomain重定向到包含HTTP的NewDomain

时间:2016-05-10 02:34:34

标签: .htaccess redirect mod-rewrite

我搜索过.htaccess,它将旧网站的所有流量重定向到新网站(包括HTTPS),但我还没找到。

其实我有这个:

RewriteEngine on

# First rule - if this is an SSL connection, then redirect
# to http://example.com then stop processing other rules
RewriteCond %{HTTPS} on
RewriteRule (.*) http://www.OldDomain.com/$1 [R=301,L]

# Second rule - all other requests, redirect to http://newdomain.com.
RewriteRule (.*) https://www.NewDomain.com/$1 [R=301,L]

这个适用于这些情况:

http://OldDomain.com/                > https://www.NewDomain.com/
http://OldDomain.com/internal/       > https://www.NewDomain.com/internal/
http://www.OldDomain.com/            > https://www.NewDomain.com/
http://www.OldDomain.com/internal/   > https://www.NewDomain.com/internal/

https://OldDomain.com/               > https://www.NewDomain.com/
https://www.OldDomain.com/           > https://www.NewDomain.com/

但这些使用HTTPS的情况并不是:

https://OldDomain.com/internal/
https://www.OldDomain.com/internal/

我的.htaccess中缺少什么?

由于

2 个答案:

答案 0 :(得分:0)

试试这个 -

  • 将它放在旧的.htaccess :(从旧到新)

    Options +FollowSymlinks
    RewriteEngine on
    rewritecond %{http_host} ^domain.com [nc]
    rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
    
  • 把它放在新的htaccess的根目录中:( http到https)

    RewriteEngine on

    RewriteCond %{HTTPS} off

    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

参考:redirect all traffic to https

答案 1 :(得分:0)

如果新域指向不同的Web服务器,您还可以使用mod-alias而不是使用mod-rewrite:

将以下行添加到olddomain / .htaccss:

Redirect 301 / https://www.newdomain.com/

这将禁止对新域的所有请求。

参考

https://httpd.apache.org/docs/current/mod/mod_alias.html