【问题标题】:Force Canonical to use HTTPS强制 Canonical 使用 HTTPS
【发布时间】: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-&gt;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


【解决方案1】:

使用 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);
        }

【讨论】:

    【解决方案2】:

    您能否在您的 .htaccess 文件中添加这两行。

    <IfModule mod_rewrite.c>
          RewriteEngine on
          RewriteCond %{HTTP_HOST} !^myproject\.
          RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
    </IfModule>
    

    它将在 https 协议上运行。

    【讨论】:

    • 我不想在 https 上运行整个网站,只是客户部分。目前我正在做一个类似于上面的重写规则,但仍然认为这应该在应用程序逻辑中处理。
    【解决方案3】:

    我相信,对于所有以 /customer 开头的 URL,这种自动重定向应该在 HTTP 守护程序级别完成。所有流行的 HTTP 服务器,如 Apache、Nginx 或 Lighttpd 都允许这样的配置。打扰网络安全的业务逻辑是不好的,imo。

    【讨论】:

      【解决方案4】:

      结合使用 ServerUrl 和 URL 视图帮助器来构建你的 URL,例如:

      <?php
      $this->getHelper('ServerUrl')->setScheme('https')
      ?>
      <a href="<?php echo $this->serverUrl($this->url('customer/default', array('controller' => 'registration', 'action' => 'index'), array('force_canonical' => true,))); ?>">Link Caption</a>
      

      这将强制链接为完全限定而不是相对链接,并将强制方案为 https,而不会导致端口指定错误的问题。

      【讨论】:

      • 我已经尝试过了,但遇到异常,帮助程序无法加载 ServerUrl。我是否需要将其作为依赖项包含在内?作为一个视图助手,我认为我不需要...
      猜你喜欢
      • 2019-05-21
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 2021-05-20
      • 2013-07-27
      • 2018-05-27
      • 2019-01-23
      相关资源
      最近更新 更多