php - 在 CodeIgniter 中使用 .htaccess 强制使用 HTTPS

标签 php .htaccess codeigniter

我现有的 .htaccess 完美地从我的 URL 中删除了 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 .

但是,如果你从地址栏中的https中选择s并删除它,index.php又回来了,再加上最后一个URL 的部分重复。如果我键入以 http:// 开头的完整 url,它会重定向并完美运行 - 只有当我将光标放在 s 前面并删除它时,它才会执行站点中断。

关于如何阻止这个新条件和规则与我现有的文件冲突有什么想法吗?

最佳答案

对于任何 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] 结束处理。

关于php - 在 CodeIgniter 中使用 .htaccess 强制使用 HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12149807/

相关文章:

php - 如何在二维数组中不重复值的情况下在另一个循环内创建一个循环

php - 在 PHP 上实时执行 git 命令

php - 拉维尔 5 : return number of affected rows MySQL

apache - 使用 .htaccess 将特定 IP 地址重定向到我主页的特殊页面

javascript - 在 Apache CentOS6 上运行的 Node JS 应用程序 - htaccess

php - 如何让 CodeIgniter 创建新文件夹?

jquery - ajaxSubmit 的回调函数不触发

php - 将数据从Mysql传输到Excel

php - mysqli::real_escape_string 的对应物?

php - 我在哪里可以找到/如何创建一个新的 HTTPD Conf