【问题标题】:Forcing HTTPS with .htaccess in CodeIgniter在 CodeIgniter 中使用 .htaccess 强制 HTTPS
【发布时间】:2012-08-22 09:40:24
【问题描述】:

我现有的.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

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

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

【问题讨论】:

  • "只有当我将光标放在 "s" 前面并删除它时,站点才会中断"
  • 致投反对票的人 - 我理解让社区保持一致,但如果您不在 cmets 中说您这样做并给出解释以帮助我纠正,那将是极不具建设性的我未来的罪行。
  • 我不知道他们(托管公司)为什么不使用合理的东西,比如RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
  • @netcoder - 是的,他们有。隐藏 index.php,它涉及与 .htaccess 编辑协同工作的 codeigniter 路由和配置编辑,这与我的 https 重定向相冲突。如果其他人有这个问题,它可能会出现在 codeigniter 上,并且该标签有助于使其可找到。我刚刚在这个问题上浪费了 10 个小时的工作日。我正在重新添加标签。
  • @ShaquinTrifonoff 他们的解释在help.pagodabox.com/customer/portal/articles/175458-adding-ssl。是的,对于警察来说,这个评论是相关的,因为链接是 pagoda 的解释和 htaccess 编辑推荐。

标签: php .htaccess codeigniter


【解决方案1】:

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

【讨论】:

  • @MikeBrant - 没有不尊重您上面的回答。在您发布帖子之前,我已经尝试将 https 重写定位在顶部,直到我意识到上面的警告,它才起作用。我确实接受了你的“!https”的想法——它并没有影响手头的问题,但它更通用。
【解决方案2】:

尝试将 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>

【讨论】:

  • 首先放置它会导致一个奇怪的、无法解析的重定向。我只能通过将两行放在 OP 中的位置来使其工作。
  • 三票赞成?这对我的情况真的不起作用。承诺。
【解决方案3】:

我的偏好是在虚拟主机中配置这种类型的重定向,而不是使用 .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>

【讨论】:

    猜你喜欢
    • 2012-01-20
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 2012-06-17
    • 2013-05-31
    • 2017-08-22
    相关资源
    最近更新 更多