登录  /  注册
首页 > php教程 > php手册 > 正文

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

php中文网
发布: 2016-06-06 19:54:04
原创
2480人浏览过

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

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

相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学