.htaccess - 对于使用 .htaccess 输入的任何 URL,强制 SSL 到 www 子域?

标签 .htaccess mod-rewrite ssl subdomain

我有一个我们称之为 example.com 的网站的“www”子域的 SSL 证书。

因此我可以强制使用 SSL,但是如果用户没有在他们的浏览器中输入“www.example.com”,SSL 证书就会显示为无效。

我希望在网站根目录下的 .htaccess 文件中使用 mod_rewrite 规则来完成此操作。

无论用户输入什么 URI(例如 example.com、www.example.com、https://example.com、example.com/folder/file.html 等),我都想强制使用 HTTPS 和 www 子域,当然保留他们提供的任何 URI。

我一直在研究 .htaccess 文件和一些规则,因为我已经做了一些研究,但我对 mod_rewrite 或 .htaccess 没有太多经验。我想我找到了 几乎 对我有用的 SVN 修订版,但不完全是。在这里:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^example.com [nc]
RewriteRule ^(.*)$ https://www.example.com/$1 [r=301,nc]

尽量不要对我的文件大笑;它主要来 self 在网上找到的示例。

感谢任何指点! =)

最佳答案

试试这个:

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301]

关于.htaccess - 对于使用 .htaccess 输入的任何 URL,强制 SSL 到 www 子域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/570148/

相关文章:

apache - 阻止代理用户从 12 :30am to 2 am

Apache RewriteCond 比较 TIME

javax.net.ssl.SSLProtocolException : The certificate chain length (11) exceeds the maximum allowed length (10)

python - Web 应用程序在 self._sslobj.do_handshake() 的 ssl.py 中挂起几个小时

php - .htaccess 文件导致 404 错误

.htaccess - 将 Apache .htaccess 规则转换为 IIS web.config 以附加 .php 文件扩展名

.htaccess - htaccess 重定向到子目录

apache - .htaccess RewriteRules 的问题

sockets - 如何与c++库websocket++建立安全套接字连接?

php - 重写 URL 的 Glype (nginx)