![](/img/trans.png)
[英]Redirect all non-www to www for both http and https with .htaccess
[英].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.