Regex 将每个非www或http域重定向到www和https
我想将所有域重定向到www和https。 比如: 致: 及 致: 这是我在htaccess中为域1编写的代码:Regex 将每个非www或http域重定向到www和https,regex,.htaccess,Regex,.htaccess,我想将所有域重定向到www和https。 比如: 致: 及 致: 这是我在htaccess中为域1编写的代码: RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} example1\.com [NC] RewriteCond %{HTTP_HOST} !^(www\.)?example1\.com$ [NC] RewriteRule ^(.*)$ https://www.example1.com/$1 [R=30
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]
我检查:
1.如果地址没有https,并且包含example1.com,如果不完全像www.example1.com或example1.com,则重定向到
2.如果地址有https,并且包含example1.com,如果不完全像example1.com,则重定向到首先使用如下正则表达式替换start: 我用C,只用正则表达式和这个想法
Regex.Replace(myString, @"^(https?:\/\/(www\.)?)?");
然后更换端部:
Regex.Replace(myString, @"\.$");
例如:
将由第一个正则表达式替换为:
https://www.example2.com.
在第二个正则表达式之后,到:
http://www.example2.com
上面的代码片段应该对您有好处。
试试看。如果您有一些问题,请在评论中告诉我,您可以使用:
# 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]
我建议您在我的答案中使用正则表达式。例如,将第5行更改为:-重写规则。*https://www.example2.com%{REQUEST_URI}[L,R]只需更改域名即可重写规则。*https://www.example100258.com%{REQUEST_URI}[L,R]它必须是一个模式。
https://www.example2.com.
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>
# 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]