【问题标题】:.htaccess to redirect from http to https.htaccess 从 http 重定向到 https
【发布时间】:2017-07-18 23:07:18
【问题描述】:

我在共享主机上有一个网站,托管默认根目录是 /public_html,但我将我的网站放在 /pulbic_html/web 以进行文件组织 p>

我已经从 cloudflare 注册了 HTTPS

我希望得到 2 个结果

  1. 访客可以输入https://example.com, https://www.example.com
  2. 访问者可以输入http://example.comhttp://www.example.com 然后重定向到https://example.comhttps://www.example.com

我当前的.htaccess

RewriteEngine On

RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L,QSA]

使用当前的.htaccess

  1. 输入http://example.com -> 工作
  2. 输入http://www.example.com -> 工作
  3. 输入https://example.com -> 工作
  4. 输入https://www.example.com -> 工作但重定向到http://example.com 并在浏览器上显示连接不安全警告

我用谷歌搜索并想出了

RewriteEngine On
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]

RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L,QSA]

用这个.ht访问所有测试结果

请帮助实现这些

  1. 输入 https://example.com, https://www.example.com -> 工作
  2. 键入http://example.com, http://www.example.com -> 重定向到https://example.comhttps://www.example.com

谢谢大家

【问题讨论】:

    标签: php apache .htaccess redirect mod-rewrite


    【解决方案1】:

    这可能会有所帮助.. 用这个更新您当前的规则:

    RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    【讨论】:

    • 用这个替换你的重写规则.. RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule "put new rule here"
    【解决方案2】:
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 2014-04-16
      • 2012-11-02
      • 2018-08-25
      • 1970-01-01
      • 2019-06-23
      相关资源
      最近更新 更多