Newfoundland
Newfoundland

Reputation: 507

Yii2 redirect all urls to https://www without using htaccess

I need to be able to redirect all urls (non www and www) to https://www in Yii2.

I want to try to do this without using .htaccess

It's not a problem to create the urls via the urlManager in Components:

urlManager => [
    'baseUrl' => 'https://www.somesite.com'
]

However, I'm struggling to do the redirects. I did take a look at this article http://www.yiiframework.com/wiki/407/url-management-for-websites-with-secure-and-nonsecure-pages - does that help in the context of Yii2?

EDIT

I made reference in my comment below that when I added $_SERVER['HTTPS'] = 'on' in the backend folder index.php, I get SSL without having to do anything with the .htaccess file which is great. This is used in the function getIsSecureConnection() in \yii2\web\Request.php

This was referenced in this article https://github.com/yiisoft/yii2/issues/9116

However, if I do the same on the frontend index.php file, I do not get the same behaviour.

For example

http://www.client-site-1.co.uk/backend => https://www.client-site-1.co.uk/backend

(I don't have a SSL on this domain at the moment!)

versus

http://www.client-site-1.co.uk => http://www.client-site-1.co.uk

Any help much appreciated !!

Upvotes: 1

Views: 2140

Answers (2)

Newfoundland
Newfoundland

Reputation: 507

I found this solution which is what I need on Yii2

Force HTTPS on Yii2, extending the Yii application class

All the credit goes to plm57

Upvotes: 1

Deep Swaroop Sachan
Deep Swaroop Sachan

Reputation: 148

You can do it with Apache without .htaccess
#
# Non www 
#
<VirtualHost *:80>
    ServerName example.com
    # Other alternatives domains:
    ServerAlias example.net www.example.net
    ServerAlias example.org www.example.org

    RedirectPermanent / http://www.example.com/
</VirtualHost>


#
# Main site - www
#
<VirtualHost *:80>
    ServerName www.example.com

    DocumentRoot /var/www/example.com/frontend/web
    # ...
</VirtualHost>

Upvotes: 0

Related Questions