gpt4 book ai didi

php - 强制 Canonical 使用 HTTPS

转载 作者:IT王子 更新时间:2023-10-28 23:54:21 31 4
gpt4 key购买 nike

我希望我的客户部分是 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 连接错误!

当我将 child_routes type 更改为 scheme 时,生成的 url 是:https://myproject.dev:80/顾客

[编辑]

作为权宜之计,如果用户试图访问非安全方案的客户部分,我将执行 htaccess 重定向:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/customer/?.*$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

我不喜欢这种方法,因为它没有必要!

[编辑]

虽然我希望我的客户部分是 https,但我不希望网站的其余部分是。

最佳答案

use Zend\Uri\UriFactory;

在您的操作之上添加以下代码或在插件中创建一个方法。

$uri = $this->getRequest()->getUri();
$uri = (string) $uri;
$url = UriFactory::factory($uri);
$http = 'http';
$http_current = $url->getScheme();
if($http == $http_current){
$url->setScheme('https');
return $this->redirect()->toUrl($url);
}

关于php - 强制 Canonical 使用 HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35082917/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com