在CodeIgniter中强制使用.htaccess进行HTTPS

时间:2012-08-27 21:23:02

标签: php .htaccess codeigniter

我现有的.htaccess完全从我的网址中删除了index.php

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|(.*)\.swf|images|robots\.txt|css|docs|cache)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 /application/errors/404.php
</IfModule>

我需要整个网站通过SSL运行,我的托管公司Pagoda Box有一个专有的.htaccess编辑,可以从http切换到https

RewriteCond %{HTTP:X-Forwarded-Proto} = http
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]

只有在第一个条件语句的末尾添加它才有效,并且它完美地工作:它将https添加到裸URL并删除index.php

但是,如果您从地址栏中的s选择https并将其删除,则index.php会返回,并且会重复该网址的最后一段。如果我输入完整的网址,从http://开始,它会重定向并完美地工作 - 只有当我将光标放在s前面并删除它才会导致网站中断。

有关如何阻止此新条件和规则与现有文件冲突的任何想法?

3 个答案:

答案 0 :(得分:3)

尝试将http放到https重定向作为第一件事。最好使用!https作为重写的条件。所以.htaccess的重写部分可能看起来像这样。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R]

#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|(.*)\.swf|images|robots\.txt|css|docs|cache)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

答案 1 :(得分:3)

对于任何CI / Pagoda Box用户,这对我有用:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

RewriteBase /

#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|(.*)\.swf|images|robots\.txt|css|js|docs|cache)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 /application/errors/404.php
</IfModule>

请注意,RewriteBase位于https重定向之后,[L]位于结束处理之后。

答案 2 :(得分:1)

我的偏好是在虚拟主机中配置这种类型的重定向,而不是使用.htaccess

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
   ServerName secure.example.com
   DocumentRoot /path_to_webroot/
   SSLEngine On
</VirtualHost>