首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >强制Canonical使用HTTPS

强制Canonical使用HTTPS
EN

Stack Overflow用户
提问于 2016-01-29 19:05:04
回答 4查看 1.3K关注 0票数 15

我希望我的客户部分是https,例如注册、登录、个人详细信息、订单详细信息等。我已将我的路由设置为包括https方案(我可以通过https访问客户部分),但我不能强制我的链接使用https:

代码语言:javascript
复制
<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方案?

代码语言:javascript
复制
'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并添加了方案选项,但这会生成以下url:https://myproject.dev:80/registration,它会在尝试连接到端口80时生成一个SSL connection error

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

编辑

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

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

我不喜欢这种方法,因为它不应该是必要的!

编辑

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

EN

回答 4

Stack Overflow用户

发布于 2016-02-09 16:31:06

使用Zend\Uri\UriFactory;

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

代码语言:javascript
复制
$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);
        }
票数 5
EN

Stack Overflow用户

发布于 2016-02-06 15:43:23

你能在你的.htaccess文件中添加这两行吗?

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

它将在https协议上运行。

票数 4
EN

Stack Overflow用户

发布于 2016-02-04 23:24:01

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35082917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档