使用htaccess将http网址重定向到https网址

使用htaccess将http网址重定向到https网址

问题描述:

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

I using below code in the htaccess file to redirect www to non www domain and the requirement as below.

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 to https://example.com
  • https://www.example.com to https://example.com

非https网址到https网址

  • http://example.com to https://example.com

其他任何非HTTP网址到https网址的页面

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

结果:


  1. 工作正常。

  1. is working fine as expected.

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

& 3 is not working and remains http url when user directly enter in browser. It should upgrade to https url.

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

Code is running in godaddy shared hosting. Any help will be much appreciated. Please feel free to let me know if any questions.

我用这个:

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]

我排除了 /。众所周知/ 来更新加密证书。

I excluded /.well-known/ to update the let's encrypt certificates.

<目录> 或用于.htaccess:

Inside a <directory> or for .htaccess:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_FILENAME} !^.well-known/
RewriteRule .* https://wiimmfi.de/$0 [R=301,L]