Php 在CodeIgniter中使用.htaccess强制HTTPS
我现有的.htaccess从我的URL中删除index.php:Php 在CodeIgniter中使用.htaccess强制HTTPS,php,.htaccess,codeigniter,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
<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>
它只有在第一个条件语句末尾添加时才起作用,并且在这一点上效果很好:它将https添加到裸URL并删除index.php
但是,如果在地址栏中从https中选择s并将其删除,index.php将返回,并重复URL的最后一段。如果我键入完整的url,以http:///开头,它会重定向并完美地工作-只有当我将光标放在s前面并删除它时,网站才会中断
关于如何阻止此新条件和规则与现有文件冲突,您有什么想法吗?尝试将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>
对于任何CI/Pagota 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]用于结束处理。我的首选是在虚拟主机中配置这种类型的重定向,而不是使用.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>
只有当我将光标放在s前面并将其删除时,网站才会与投票被否决的人发生冲突-我理解让社区保持一致,但如果你不在评论中说是你做的,并给出解释来帮助我在将来纠正我的错误行为,那是非常没有建设性的。我不知道为什么托管公司没有使用一些合理的东西,比如RewriteEngine On RewriteCond%{HTTPS}off RewriteRule^HTTPS://%{HTTP_HOST}%{REQUEST_URI}。@netcoder-是的,是的。隐藏index.php涉及codeigniter路由和配置编辑与.htaccess编辑协同工作,这与我的https重定向相冲突。如果其他人有这个问题,它可能会在codeigniter上,标签有助于找到它。我只是在这个问题上浪费了10个小时的工作时间。我正在添加标签。@ShaquinTrifonoff他们的解释已打开。是的,对于警方来说,这个评论是相关的,因为链接是宝塔的解释和htaccess编辑建议。将它放在第一位会导致一个奇怪的、无法解决的重定向。我只能把这两行放在OP.three UpVote中的位置来实现这一点?这对我的病人来说真的不管用。承诺。@MikeBrant-不要对上面的回答不尊重。在你发表文章之前,我已经尝试将https重写放在顶部,直到我意识到上面的警告,它才起作用。我确实拿走了你的!https的想法虽然没有影响手头的问题,但它更通用。
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName secure.example.com
DocumentRoot /path_to_webroot/
SSLEngine On
</VirtualHost>