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 的相关文章

  • 自定义 WP 主题时,我应该将导航栏放在“”标签之前还是之后?

    我正在通过制作子主题来自定义 WP 主题 我将 Bootstrap 中的导航栏放入子主题目录中的 header php 文件中 但是 我不确定在哪里放置导航栏代码 我可以把它都放在前面and之后标记成功 例如 无论我选择哪一个 导航栏都显示
  • 如何在 Laravel 查询中使用多个 OR,AND 条件

    我需要 Laravel 查询帮助 我的自定义查询 返回正确结果 Select FROM events WHERE status 0 AND type public or type private 如何写这个查询Laravel Event w
  • 响应 301 永久移动

    我曾经得到以下对 php 请求的响应 回复
  • SMTP 配置在生产中不起作用

    我正在尝试在提交表单时发送电子邮件 我正在使用 PHPMailer 使用以下配置发送邮件 mail new PHPMailer mail gt isSMTP mail gt Host mail example in mail gt Port
  • PHP 或 WAMP 不确定是什么

    我已经安装了 WAMP 服务器 2 0 PHP 5 4 3 安装WAMP后我已经重新启动了所有服务并且可以打开 phpinfo 显示良好 phpmyadmin 它也显示得很好 我可以使用数据库 然而 当在 Chrome 中运行简单的 php
  • 当sql连接中存在两个同名列时,如何从一个表列中获取值

    当我连接两个具有相同名称列的表时 我目前面临着尝试获取值的问题 例如 table1 date和table2 date 每个表中的日期不同 我将如何获取 日期 本例中的表1 我目前正在跑步 while row mysqliquery gt f
  • 通过 facebook graph API 检索 facebook 用户的邮政编码

    我正在尝试使用 facebook graph API 检索用户的邮政编码 我正在使用以下代码 代码在php ini中 facebook new Facebook array appId gt APP ID secret gt APP SEC
  • 在 Symfony 序列化中更改序列化属性名称

    我正在使用 Symfony 序列化器 效果很好 use Symfony Component Serializer Annotation Groups Groups default notification public function g
  • PHP:读取字体文件的 TrueType/OpenType 元数据

    如何阅读字体详细信息 例如 字体在其元数据中包含版权 姓氏 设计者 版本等信息 我还希望脚本能够计算文件中的字形数量 并返回字体支持的语言 例如 典型的字体可能包含西方语言 瑞典语和罗马语言支持 并具有数百个字形 它应该支持 truetyp
  • 将 Google 信任徽章添加到 Magento

    我正在尝试将 Google Trust Badge 添加到我的 magento 商店 我尝试在 Magento 网站上搜索扩展程序 但找不到 我是否需要将以下代码粘贴到产品和结账页面 还是必须对其进行更改 如果有人能引导我走向正确的方向 我
  • Magento - 检查 cms 页面

    我想通过 php 检查页面是否是 Magento 中的 cms page 我需要不同的 cms 页面面包屑 所以我尝试在一个条件下做到这一点 但我不知道如何或在哪里查看 到目前为止 这是我的 breadcrumbs phtml p some
  • 当会话令牌无效时,我应该使用什么状态代码?

    创建 Web 服务 RESTful 时 当会话令牌无效时我应该使用什么状态代码 目前我公司的人给我发了一个404 未找到 但我认为这是不正确的 因为资源存在 也许我应该使用 401 Unauthorized 你怎么认为 您建议我在这种情况下
  • MySQL PHP邮政编码比较具体距离

    我试图找出比较一个邮政编码 用户提供的 和一大堆其他邮政编码 现在大约有 200 个邮政编码 之间的距离的最有效方法 相对于加载时间 但它会随着时间的推移而增加 我不需要任何精确的东西 只是在球场上 我下载了整个美国的邮政编码 csv 文件
  • Apache、PHP 和 MySQL 可移植吗?

    我可以在外部硬盘上运行 Apache PHP 和 MySQL 吗 我需要这个 因为我在不同的地方工作 计算机 有时我没有安装和配置所有使用的应用程序 当然可以 XAMPP http www apachefriends org en xamp
  • Sonata DateTimePickerType 类默认日期显示错误的日期时间格式

    我陷入困境 我不知道如何使用 sonata DateTimePickerType 类正确设置默认日期和时间 我尝试了不同的方法 但到目前为止 没有一种方法没有帮助 在下面的截图中 help 键显示正确的日期和时间 但是当我使用 dp 默认日
  • 检查条件并通过 Zend 中的 Regex 识别 url 中的模式

    我正在实现 Zend Regex 路由 并且必须对 url 执行多次检查 例如 如果这是我的网址 http localhost application public index php module controller action 这是
  • 有没有办法使用 ASP.NET 在用户离开页面时始终运行某些服务器端代码?

    我想知道当用户离开 ASP NET 中的页面时是否有任何方法可以始终运行一些服务器端代码 页面卸载事件不好 因为如果有人单击链接 则不会调用该事件 理想情况下 即使用户关闭浏览器 我也希望代码能够运行 我怀疑我所问的问题是不可能的 但问一下
  • URL 中的 %2F 中断并且未引用所需的 .php 文件 [重复]

    这个问题在这里已经有答案了 我需要将 作为变量作为 URL 的一部分传递 我的结构如下所示 www domain com listings page 1 city Burnaby South type Townhome bedroom 2
  • 保存多对多关系,同步/附加不存在?

    我有以下两个多对多关系的模型 use Illuminate Database Eloquent Model class Permission extends Model The database table used by the mode
  • 为什么我的会话仍然存在?

    我一定很愚蠢 因为似乎一件相当明显的事情现在让我完全困惑 我有一个会议 ie SESSION handbag id 在某个时刻 我需要彻底终止这个会话 ie at the start of the page session start el

随机推荐

  • 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