簡體   English   中英

強制Canonical使用HTTPS

[英]Force Canonical to use HTTPS

我希望我的客戶部分是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/registrationhttp://myproject.dev/customer/registration

我想要的是: https://myproject.dev/customer/registrationhttps://myproject.dev/customer/registration

它似乎使用當前的方案 - 所以如果我在http上,那么網址是http,如果我在https上,那么網址是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 ://myproject.dev:80 / https://myproject.dev:80/registration ,因為它正在嘗試生成SSL connection error連接到端口80!

當我將child_routes type更改為scheme ,生成的url是: https://myproject.dev:80/customerchild_routes

[編輯]

作為一個臨時解決方案,如果用戶嘗試訪問非安全方案的客戶部分,我正在進行htaccess重定向:

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

我不喜歡這種方法,因為它不應該是必要的!

[編輯]

雖然我希望我的客戶部分是https,但我不希望網站的其余部分。

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

你能否在你的.htaccess文件中添加這2行。

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

它將在https協議上運行。

我相信,這種自動重定向應該在HTTP-daemon級別上為所有以/ customer開頭的URL完成。 所有流行的HTTP服務器,如Apache,Nginx或Lighttpd都允許這樣的配置。 imo,打擾網絡安全的業務邏輯並不好。

使用ServerUrl和URL視圖助手的組合來構建您的URL,EG:

<?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,而不會導致指定錯誤的端口問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM