【发布时间】:2016-05-07 02:14:45
【问题描述】:
我希望我的客户部分是 https,例如注册、登录、个人详细信息、订单详细信息等。我已经设置了包含 https 方案的路线(我可以通过 https 访问客户部分)但我不能强制我的链接使用 https:
<a class="register" href="<?php echo $this->url('customer/default', array('controller' => 'registration', 'action' => 'index'), array('scheme' => 'https', 'force_canonical' => true,)); ?>">Register</a>
我得到了什么:http://myproject.dev/customer/registration
我想要的:https://myproject.dev/customer/registration
它似乎正在使用当前方案 - 所以如果我使用的是 http,那么 url 就是 http,如果我使用的是 https,那么 url 就是 https。
如何强制$this->url 一直使用https 方案?
'router' => array(
'routes' => array(
'customer' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/customer',
'scheme' => 'https',
'defaults' => array(
'__NAMESPACE__' => 'Customer\Controller',
'https' => true,
'controller' => 'Index',
'action' => 'index',
),
),
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'scheme' => 'https',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
[编辑]
myproject.dev 是我的本地计算机,我在其中将主机更改为 vhosts 文件。我已将我的 vhosts 文件设置为接受 ssl,这不是问题。
我已将路由类型更改为 Zend\Mvc\Router\Http\Scheme 并添加了 scheme 选项,但会生成以下 url:https://myproject.dev:80/registration 在尝试连接到端口 80 时生成 SSL connection error!
当我将child_routes type 更改为scheme 时,生成的网址为:https://myproject.dev:80/customer
[编辑]
作为权宜之计,如果用户试图通过非安全方案访问客户部分,我将执行 htaccess 重定向:
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/customer/?.*$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
我不喜欢这种方法,因为它没有必要!
[编辑]
虽然我希望我的客户部分是 https,但我不希望网站的其余部分是。
【问题讨论】:
-
您有两种选择:1) 使用 Apache 重定向,或 2) 使用 PHP 重定向。但我首先要问为什么你只想要你网站的一部分 HTTPS。简单地确保整个网站的安全对您的用户更友好。
标签: php zend-framework zend-framework2 view-helpers zend-view