首页 > 解决方案 > 如何使用 htaccess 文件将 http 重定向到 https://www.example.com?

问题描述

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

我已经使用Codeigniter框架创建了一个网站,并且我的域和托管在GoDaddy. 现在,我已经安装SSL certificate并管理了我的网站。现在,当我example.com在 URL 中使用它时,它会重定向我,https://www.example.com但是当我点击我的login页面时,它会显示我Not Found并且 URL 看起来像https://www.example.com/index.php?/login,但我想要 URL Like https://www.example.com/login。那么,我该怎么做呢?请帮忙。

谢谢你

标签: .htaccesscodeigniterssl-certificate

解决方案


  1. 更改规则的顺序
  2. 有一个规则来重定向wwwhttp -> https

你的 .htaccess 应该是这样的:

RewriteEngine on

# redirect for adding www and https
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

确保使用新的浏览器进行测试。


推荐阅读