HTTP/HTTPS, without index.php, using htaccess, plus XHR

2023-05-16


http://ellislab.com/forums/viewthread/86113/


Removing index.php and forcing HTTP/HTTPS

I have read many posts about people trying to force HTTPS for some views and returning to HTTP for others. I struggled with this for a while too but I think this solution is pretty solid.

First of all, having your base_url automatically adjust between http and https makes everything much easier. This way all your base_url() and site_url() calls have the proper protocol.

$config['base_url'"http".((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'== "on") ? "s" "")."://".$_SERVER['HTTP_HOST'].str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']); 

Starting with the usual htaccess file:

<IfModule mod_rewrite.c>
    
RewriteEngine on
    Options 
+FollowSymLinks
    RewriteBase 
/
    
    
RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond 
%{REQUEST_FILENAME} !-d
    RewriteRule 
^(.*)$ index.php/$1
</IfModule>

<
IfModule !mod_rewrite.c>
    
ErrorDocument 404 /index.php
</IfModule

You can then check whether HTTPS is on or not with:

RewriteCond %{HTTPS} off
RewriteCond 
%{HTTPS} on 

For example, to force HTTPS on all pages you could use the following:

RewriteCond %{HTTPS} off
RewriteRule 
^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301] 

To force HTTPS on some pages:

RewriteCond %{HTTPS} off
RewriteCond 
%{REQUEST_URI} (auth|register|secure)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301] 

To return back to HTTP:

RewriteCond %{HTTPS} on
RewriteRule 
^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301] 

To return back to HTTP on all other pages, you need to add exceptions for the pages that are secure:

RewriteCond %{HTTPS} off
RewriteCond 
%{REQUEST_URI} (auth|register|secure)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301]

RewriteCond %{HTTPS} on
RewriteCond 
%{REQUEST_URI} !(auth|register|secure)
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301] 

To avoid a partially encrypted page, you need to add exceptions for any other URIs you might use such as your images or scripts folder. I like to place everything in a folder called ‘static’ (‘static/images’, ‘static/js’, etc) so I only add one exception for that.

RewriteCond %{REQUEST_URI} !(static|auth|register|secure

The finished product:

<IfModule mod_rewrite.c>
    
RewriteEngine on
    Options 
+FollowSymLinks
    RewriteBase 
/
    
    
RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond 
%{REQUEST_FILENAME} !-d
    RewriteRule 
^(.*)$ index.php/$1

    RewriteCond 
%{HTTPS} off
    RewriteCond 
%{REQUEST_URI} (auth|register|secure)
    
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301]

    
RewriteCond %{HTTPS} on
    RewriteCond 
%{REQUEST_URI} !(static|auth|register|secure)
    
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301]
</IfModule>

<
IfModule !mod_rewrite.c>
    
ErrorDocument 404 /index.php
</IfModule


HTTPS and XmlHttpRequests (Ajax)

Not only do XHR calls throw security errors when you try to load content between domains but also between HTTP and HTTPS. Secondly, the headers passed by apache allow browsers to automatically redirect but the XmlHttpRequest object does not.

To solve this you would have to add an exception for any URI that you planned on accessing from one protocol to another.

Example:

RewriteCond %{REQUEST_URI} !(static|auth|register|secure|categories/get_list|products/get_types)

<?=site_url('categories/get_list');?> 

I quickly found out that this became tedious and confusing when I had a lot of requests in a secure environment. Routes to the rescue!

By adding the following to your routes file:

$route['xhr/(:any)''$1'

And adding ‘xhr’ to your list of exceptions; you can now call any URI within your application without changing protocols while still allowing the browser to view that controller using another protocol.

RewriteCond %{REQUEST_URI} !(static|xhr|auth|register|secure)

<?=site_url('xhr/categories/get_list');?> 

I hope this has been helpful!

Phil

 
 
Dylan
Posted: 09 December 2008 08:08 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2008-01-22
2 posts

Might be a bit late, but this is quite informative. Good post. =]

 
 
vlad_ci
Posted: 10 December 2008 02:45 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2008-07-16
40 posts

thank you for bumping this up—I have not seen this post and it is exactly what I needed

 
 
terminate
Posted: 18 June 2009 05:16 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2007-10-27
6 posts

Phil_B’s .htaccess is great and I’m an active user of it!

I would just drop my two cents. If you follow the rules straight all your static content will be served non SSL which is actually good for performance but will drop a Firefox alert (not all content is encrypted) and you will not get the blue/green bar.

Just a small change to make the static content, under encrypted pages, be encrypted served.

Also added the [L] (last) modifier to make the server stop processing after one of the bottom two requests (should make it micro, mini, faster).

RewriteEngine on
Options 
+FollowSymLinks

RewriteCond 
%{REQUEST_FILENAME} !-f
RewriteCond 
%{REQUEST_FILENAME} !-d
RewriteRule 
^(.*)$ index.php/$1

RewriteCond 
%{HTTPS} off
RewriteCond 
%{REQUEST_URI} (auth|register|secure|payment)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond 
%{REQUEST_FILENAME} !-f
RewriteCond 
%{REQUEST_FILENAME} !-d
RewriteCond 
%{REQUEST_URI} !(static|auth|register|secure|payment)
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] 

Thanks for your code Phil_B!

Frankie


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

HTTP/HTTPS, without index.php, using htaccess, plus XHR 的相关文章

  • CURL请求问题

    我正在尝试验证 paypal pdt 信息 我生成了模型表单并提交了它 IT 部门也开始工作并返回了信息 我尝试了同样的事情来发出卷曲请求 但我的当前请求对我来说返回空白 我的模型形式
  • 使用 php 将 HTML 输出转换为纯文本

    我正在尝试将示例 HTML 输出转换为纯文本 但我不知道如何操作 我使用 file get contents 但我尝试转换的页面返回的结果最相似 raw http localhost guestbook profiles php file
  • 使用PHP获取http url参数而不自动解码

    我有一个像这样的网址 test php x hello world y 00h 00e 00l 00l 00o 当我将它写入文件时 file put contents x txt GET x gt hello world file put
  • 使用 PHP Mcrypt 加密并使用 MySQL aes_decrypt 解密?

    是否可以使用 PHP 加密数据mcrypt并用MySQL在数据库中解密AES DECRYPT 目前 我正在使用RIJNDAEL 128 for mcrypt关于 PHP 我还确保数据库中的加密字段具有数据类型blob Yet AES DEC
  • 如果 POST 成功但没有创建任何新内容,我们应该返回什么状态代码?

    我们有一个端点 当您发布创建新版本的资源时 它会返回 201 和新创建的资源的位置 它根据当前版本和发布的版本 使用类似 semver 的规则集 的比较来确定新版本号 如果您发布的版本与现有版本相同 则不会更新版本号 在这种情况下我们应该返
  • php 时间戳 UTC

    我有一个 PHP MySQL 查询 它将一些数据插入 MySQL 数据库 并且包含时间戳 目前INSERT查询用途NOW 对于时间戳列 它以以下格式保存在数据库中 2012 07 24 13 13 02 不幸的是 对我来说 服务器不在我的时
  • 如何使用 PHP 发送 OPTIONS 请求

    有谁知道如何使用 PHP 发送 OPTIONS 请求 我找不到执行此操作的curl setopt 我正在使用 php 5 6 7 我已经弄清楚了 GET POST DELETE 和 PUT 只需要选项 我已经尝试过以下 hd 的答案 ch
  • 如何更新 mongodb PHP 中的所有文档

    我正在设置一个 cronjob 来更新该字段views 15关于集合中的所有文档query 这就是我所拥有的 应该有效 update array set gt array views 15 gt 0 db gt queries gt upd
  • Symfony 不会从集合中删除实体

    我知道一般来说有很多关于这个主题的帖子 不幸的是 这些大多涉及对数据库的实际持久操作 就我而言 我在持久操作之前发生了一个问题 我有一个带有实体 学说 持久性集合的表单 您可以通过 javascript 从 DOM 中删除 对象 提交后 当
  • SQL 查询:按 ntext 字段分组

    我有以下查询 它基本上检索销量最高的 5 本书 select top 5 count id book orddetails books sold bk from orderdetails orddetails ord inner join
  • Facebook 页面插件无法运行 - 仅适用于一个网站

    我已将页面插件集成到我的目录中 ruhrlink de info php schluessel 150991 不起作用 其他的可以 当我在开发者网站 gt 页面插件中手动输入网址时 它在那里也不起作用 https www facebook
  • stdClass 类的对象无法转换为字符串

    我现在在使用 PHP 时遇到问题 收到此错误 Object of class stdClass could not be converted to string当我在我的网站中运行这部分代码时发生错误 function myaccount
  • PHP:将 UTC 时间更改为太平洋时间(PST/PDT)[重复]

    这个问题在这里已经有答案了 我有一个 Datetime 对象并通过以下方式获取时间 today date Y m d H i s temp date gt getTimeStamp 我希望能够将其转换为加利福尼亚州的当前时间 但我需要考虑
  • Http Auth 不适用于 PHP

    我使用 Laravel Lumen Shield 扩展进行 Http 身份验证 但是在我的本地计算机上一切都很完美 我只在我们的服务器上遇到了问题 问题是在我提交正确的登录数据后 登录屏幕再次出现 我尝试了不同的登录数据 不同的浏览器 登录
  • 如何在 php 数组中添加条件?

    这是数组 anArray array theFirstItem gt a first item if True conditionalItem gt it may appear base on the condition theLastIt
  • 如何安装 php 5.3.14 ubuntu 12.10

    我必须在我的 ubuntu 12 10 上安装这个特定版本才能与提供商保持兼容 我可以使用 synaptic 轻松安装 php 5 3 10 但无法升级到 5 3 14 我怎样才能做到这一点 apt get 不起作用 我在网上看到了几个教程
  • 在 Codeigniter 中添加表前缀以加入

    我设置了 Codeigniter 将前缀 kms 添加到我的活动记录查询中 但是 我尝试使用两个 ON 条件进行连接 但它不会将它们放在前面 现在我必须像这样手动添加它们 this gt db gt join site items kms
  • 未捕获的异常:无法找到 Mix 文件

    我正在尝试在本地系统中运行 laravel 应用程序 我已遵循https gist github com hootlex da59b91c628a6688ceb1 https gist github com hootlex da59b91c
  • PHP - 如何查看服务器是否支持 TLS 1.0?

    我正在编写一个简单的检查器 您可以在其中输入一个 URL 该检查器将检查输入的 URL 是否使用 TLS 1 0 1 1 或 1 2 本质上 我想显示一条消息 Yoursite com 正在使用 TLS 1 0 建议禁用此功能 问题是 只有
  • 在 CakePHP 中访问 Configuration::read 控制器

    我的 CakePHP 应用程序有一个单独的配置文件 该文件加载在 bootstrap php 中 我的问题是 如何访问控制器中的配置变量 IE 如何在控制器中执行Configure read variable 函数 谢谢 在我的自定义配置文

随机推荐

  • Html5 Geolocation获取地理位置信息

    http www cnblogs com lwbqqyumidi archive 2012 11 10 2764352 html Html5中提供了地理位置信息的API xff0c 通过浏览器来获取用户当前位置 基于此特性可以开发基于位置的
  • openfire整合外部数据库的方法

    http www igniterealtime org builds openfire docs latest documentation db integration guide html 看了这篇教程 xff0c 发现了一个问题 xff
  • 金融机构如何应对核心系统分布式智能化升级大潮?

    过去40多年 xff0c 中国金融业实现了技术上的引进 借鉴 xff0c 并逐渐开始进行原创性创新 比如 xff0c 在 支付系统建设方面 xff0c 我国现在就走在了世界的前列 从二代大小额支付系统CNAPS到跨境人民币支付系统CIPS再
  • ajax请求中session无效的问题

    遇到一个问题 xff0c 发现网站中的所有ajax在某个服务器中的session总是无效 xff0c 后来同事查了资料 xff0c 原来php的配置文件中有个选项 xff1a Whether or not to add the httpOn
  • 解决seesion在二级域名下无效的问题

    开发中遇到了一个问题 xff0c 当用户在www aa com登陆了 xff0c 在二级域名下的登陆无效 例如 aa com 后来检查了很久 xff0c 终于知道了问题所在 xff0c 在www aa com下生成的cookie不适用于 a
  • 提供全球商家信息的网站

    做LBS的应用 xff0c 商家信息的获取和维护是个很重要的问题 xff0c 在中国的某些大型网站是雇佣了兼职人员去维护这些数据 xff0c 但对于小公司来说这种方法是不现实的 现在发现了一个网站 xff0c 提供了全球的商家信息 xff0
  • 使用web端连接xmpp

    在apache的配置文件中加入下面3句 xff1a ProxyRequests Off ProxyPass xmpp httpbind http 127 0 0 1 7070 http bind ProxyPassReverse xmpp
  • ubuntu apache开启重写模块

    http www iblue cc 2011 09 ubuntu apache E5 BC 80 E5 90 AF E9 87 8D E5 86 99 E6 A8 A1 E5 9D 97 Ubuntu下apache2的rewrite模块默认
  • openfire xmpp 如何判断用户是否在线

    http iammr 7 blog 163 com blog static 49102699201041961613109 想象中如此简单的功能 xff0c 想不到却这般大费周折 如要实现 xff0c 必须先确保 xff1a 1 openf
  • sql 分组统计

    原始的数据结构是这样的 xff1a 这是一个信息表 xff0c 记录下每个app每天对应什么等级 现在需求是 xff1a 统计每天每个等级有多少个app xff1f 实现的sql如下 xff1a select count as num le
  • Errors running builder JavaScript Validator的问题

    http jc dreaming iteye com blog 1038995 最近使用eclipse时 xff0c 在编译项目总是出现问题 Errors occurred during the build Errors running b
  • coreseek索引更新机制

    k索引更新机制 版权声明 xff1a 转载时请以超链接形式标明文章原始出处和作者信息及本声明 http fatal blogbus com logs 45153968 html 61 61 xff0c 昨晚太晚睡觉 xff0c 所以日记又没
  • golang生成自定义标签名(带CDATA标识)的xml

    在golang中 xff0c 有时候需要生成带CDATA标识的xml值 xff0c 例如这种 xff1a lt xml version 61 34 1 0 34 gt lt xml gt lt to User gt lt CDATA use
  • 有人痴狂,有人跑路,开源软件新一年的冰火两重天

    最近有关开源软件的话题始终占领着IT界的新闻头条 xff0c Log4j开源软件的惊天漏洞 xff0c 才刚刚出现不久 xff0c Fake js的作者也惊天删库跑路了 xff0c CurL的作者怒怼苹果只会白嫖开源却不出力 xff0c L
  • linux下通过ssh用户名密码的rsync传输文件方法

    一般用rsync传输文件都会使用密钥的方式实现免密码验证 xff0c 但有些机器由于特殊的原因 xff0c 不能配置密钥 xff0c 这时就要用ssh的用户名和密码方式使用rsync 1 首先 xff0c 通过ssh 命令登录一次远程的主机
  • codeigniter验证码类库

    http hi baidu com mediumgirl item c734b8f5a1cacfc3a835a2ae 折腾了我四五个小时 xff0c 终于 xff0c ci的验证码类库成功的整出来了 下面请看源码 xff1a 在applic
  • golang json.Marshal 特殊html字符被转义解决方案

    pages goods goods gid 61 56 amp code 61 1 会在转json中变成pages goods goods gid 61 56 u0026code 61 1 解决方案 xff1a content 61 str
  • mongodb 错误src/mongo/db/query/plan_enumerator.cpp的修复

    某个mongodb 3 2的库执行下面的查询就报错 xff1a db 34 xxxx 34 find 34 createdAt 34 34 gte 34 34 2019 04 23T00 00 00 43 08 00 34 34 lte 3
  • MySQL新建用户,授权,删除用户,修改密码

    http www cnblogs com analyzer articles 1045072 html grant all privileges on test to test 64 96 96 identified by 39 1234
  • HTTP/HTTPS, without index.php, using htaccess, plus XHR

    http ellislab com forums viewthread 86113 Removing index php and forcing HTTP HTTPS I have read many posts about people