Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Change all base_urls of your Magento 2 live shop to https on hypernode
#!/usr/bin/env python
"""
Set the base-urls for your Magento 2 installation to support only https.
To use, download the file and make it executable. Then run:
./change_magento2_base_urls_to_https.py
After use, check your base-urls by issuing:
n98-magerun2 sys:store:config:base-url:list
This script requires n98-magerun2.
"""
import json
import subprocess
import os
import sys
from urlparse import urlsplit
def securify(url):
url = urlsplit(url)
return "https://{}/".format(url.netloc)
def set_base_url(data, setting):
scope_id = data['Scope-ID']
scope = data['Scope']
value = securify(data['Value'])
command = '/usr/local/bin/n98-magerun2 --root-dir=/data/web/magento2 config:store:set --scope="{}" --scope-id="{}" "{}" "{}"'.format(scope, scope_id, setting, value)
subprocess.check_output(command, shell=True)
def main():
if not os.path.isfile('/data/web/magento2/app/etc/env.php'):
print >>sys.stderr, "No magento 2 installation found in /data/web/magento2"
sys.exit(1)
command = '/usr/local/bin/n98-magerun2 --root-dir=/data/web/magento2 config:store:get "web/unsecure/base_url" --format=json'
magerun_json = subprocess.check_output(command, shell=True)
magerun_dict = json.loads(magerun_json)
for setting in 'web/unsecure/base_url', 'web/secure/base_url':
for value in magerun_dict.values():
set_base_url(data=value, setting=setting)
subprocess.check_output('/usr/local/bin/n98-magerun2 --root-dir=/data/web/magento2 cache:flush', shell=True)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.