.htaccess 重定向 https 并自动添加 www。如果不存在子域
.htaccess redirect https and automatically add www. if no subdomain exists
需要帮助,
对于我在 Planethoster 托管的网站,我试图强制 HTTP 到 HTTPS 而没有 www. 自动与 https://www.
在 .htaccess 中使用此代码
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [L,R=301]
但是当我只使用 url mydomain.com 而没有 www. 时出现问题 returns一个奇怪的url:
https://www.mydomain。 com/https:/我的域名。 com/(com 前没有空格)
所以我无法通过 mydomain.com 访问我的站点,我必须手动输入 www.
有人可以帮助我吗?
如果没有其他子域,则强制使用 SSL 并要求使用 www。
你可以试试这个,
# if ssl is off and starts with www or no subdomain,
# redirect to https://www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.example.com [OR]
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
# if ssl and no subdomain, redirect to www.
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
# if ssl off, and there's a subdomain that's not www,
# just use the current subdomain but redirect to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ^(.*)\.example.com
RewriteRule ^ https://%1.example.com%{REQUEST_URI} [L,R=301]
你可以在这里测试它们,https://htaccess.madewithlove.be
这些应该涵盖所有场景,
需要帮助,
对于我在 Planethoster 托管的网站,我试图强制 HTTP 到 HTTPS 而没有 www. 自动与 https://www.
在 .htaccess 中使用此代码
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [L,R=301]
但是当我只使用 url mydomain.com 而没有 www. 时出现问题 returns一个奇怪的url: https://www.mydomain。 com/https:/我的域名。 com/(com 前没有空格)
所以我无法通过 mydomain.com 访问我的站点,我必须手动输入 www.
有人可以帮助我吗?
如果没有其他子域,则强制使用 SSL 并要求使用 www。 你可以试试这个,
# if ssl is off and starts with www or no subdomain,
# redirect to https://www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.example.com [OR]
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
# if ssl and no subdomain, redirect to www.
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
# if ssl off, and there's a subdomain that's not www,
# just use the current subdomain but redirect to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.example.com
RewriteCond %{HTTP_HOST} ^(.*)\.example.com
RewriteRule ^ https://%1.example.com%{REQUEST_URI} [L,R=301]
你可以在这里测试它们,https://htaccess.madewithlove.be
这些应该涵盖所有场景,