简体   繁体   English

将每个域非 www 或 http 重定向到 www 和 https

[英]Redirect each domain non-www or http to www and https

I want to redirect all each domain to www and https.我想将所有域重定向到 www 和 https。
like:喜欢:

http://example1.com  
http://example1.com.  
http://www.example1.com  
http://www.example1.com.  
https://example1.com  
https://example1.com.  
https://www.example1.com.  

To:到:
https://www.example1.com https://www.example1.com

AND

http://example2.com  
http://example2.com.  
http://www.example2.com  
http://www.example2.com.  
https://example2.com  
https://example2.com.  
https://www.example2.com.  

To:到:
https://www.example2.com https://www.example2.com

This is my code for domain1 in htaccess:这是我在 htaccess 中 domain1 的代码:

    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} example1\.com [NC]
    RewriteCond %{HTTP_HOST} !^(www\.)?example1\.com$ [NC]
    RewriteRule ^(.*)$ https://www.example1.com/$1 [R=301,L]

    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} example1\.com [NC]
    RewriteCond %{HTTP_HOST} !^example1\.com$ [NC]
    RewriteRule ^(.*)$ https://www.example1.com/$1 [R=301,L]

I check :我检查:
1. if address not has https AND if contains example1.com AND if not fully like www.example1.com or example1.com THEN redirect to https://www.example.com 1. 如果地址没有 https 并且如果包含 example1.com 并且如果不完全像 www.example1.com 或 example1.com 那么重定向到https://www.example.com
2. if address has https AND if contains example1.com AND if not fully like example1.com THEN redirect to https://www.example.com 2. 如果地址有 https AND 如果包含 example1.com AND 如果不完全像 example1.com 那么重定向到https://www.example.com

First replace the start by using Regex like this:首先使用 Regex 替换开始,如下所示:

(I used C#, use just the regexes and the idea). (我使用 C#,只使用正则表达式和想法)。

Regex.Replace(myString, @"^(https?:\/\/(www\.)?)?");

then replace the end:然后替换结尾:

Regex.Replace(myString, @"\.$");

For example:例如:

Example Here示例在这里

http://example2.com. 

will be replaced by the first Regex to:将被第一个 Regex 替换为:

https://www.example2.com. 

and after the second Regex, to:在第二个正则表达式之后,到:

http://www.example2.com
<IfModule mod_rewrire.c>
RewriteEngine On
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.example1.com%{REQUEST_URI} [L,R]
</IfModule>

The above code snippet should work good for you.上面的代码片段应该对你有用。 Try it.尝试一下。 If you have some issues, let me know in the comment如果您有任何问题,请在评论中告诉我

You can use:您可以使用:

# domain example1.com
RewriteCond %{HTTP_HOST} example1\.com [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example1.com%{REQUEST_URI} [L,R=301]

# domain example2.com
RewriteCond %{HTTP_HOST} example2\.com [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example2.com%{REQUEST_URI} [L,R=301]

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

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