使用 htaccess 将 http url 重定向到 https url

2024-02-02

我在 htaccess 文件中使用以下代码将 www 重定向到非 www 域,要求如下。

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]


RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]

要求:

  1. WWW 到非 WWW

    • http://www.example.com http://www.example.com to https://example.com https://example.com
    • https://www.example.com https://www.example.com to https://example.com https://example.com
  2. 非 https url 到 https url

    • http://example.com http://example.com to https://example.com https://example.com
  3. 任何其他页面非 http url 到 https url

    • http://example.com/blog/7-best-places-to-visit http://example.com/blog/7-best-places-to-visit to https://example.com/blog/7-best-places-to-visit https://example.com/blog/7-best-places-to-visit

Result:

  1. 工作正常如预期。

  2. &3 不起作用,当用户直接在浏览器中输入时仍保留 http url。它应该升级到 https url。

代码在 godaddy 共享主机中运行。任何帮助都感激不尽。如有任何疑问,请随时告诉我。


我用这个:

RewriteEngine On
# the following line is only needed, if HTTP and HTTPS share the same config
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_FILENAME} !^/.well-known/
RewriteRule .* https://wiimmfi.de$0 [R=301,L]

我排除了/.well-known/更新让我们加密证书。

里面一个<directory>或者对于 .htaccess:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_FILENAME} !^.well-known/
RewriteRule .* https://wiimmfi.de/$0 [R=301,L]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 htaccess 将 http url 重定向到 https url 的相关文章

随机推荐