繁体   English   中英

.htaccess 重定向到新域,路径完整,适用于 www 和非 www 以及 https 和非 https

[英].htaccess redirect to new domain with path intact for both www and non-www as well as https and non https

基本上我需要一个 .htaccess 文件,它将所有流量重定向到我们的新域。 它需要在以下条件下工作:

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

(请注意在上述情况下 https 重定向到 http - 这不是问题)

还有:

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

我差点就被第一个重定向www.olddomin.com的HTTPS版本的HTTP版本工作www.olddomain.com然后重定向到新域名的HTTP版本,我的问题是与非www https://olddomain.com版本,它重定向到http://olddomain.com然后停止。

我正在使用的代码是:

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]

这几乎有效,除了https://olddomain.com/path/file.php只是重定向到http://olddomain/path/file.php并停止并且不会被重定向到http://newdomain.com/path/file.php

任何帮助将不胜感激。

您只需要在 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中的 first (...)中捕获的值。 那将是www或空白

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM