如何使用htaccess文件将http重定向到https://www.example.com?

时间:2018-11-26 10:02:03

标签: .htaccess codeigniter ssl-certificate

<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并通过我的网站进行管理。现在,当我在URL中使用example.com时,它会在https://www.example.com上重定向我,但是当我单击login页面时,它会显示Not Found,URL看起来像https://www.example.com/index.php?/login但我想要类似https://www.example.com/login的URL。那么,我该怎么做?请帮忙。

谢谢

3 个答案:

答案 0 :(得分:0)

  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]

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

答案 1 :(得分:0)

请使用此htaccess规则将100%有效

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
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]

答案 2 :(得分:0)

使用此:

Options All -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]