Hi all,

I have an easy question.

Looking for a way to change all the urls in the database from http to https. Basically we have hundreds on href links where the refernced image has a link such as http://www.domain.com/path-to-file.jpg

And need to change it to: https://www.domain.com/path-to-file.jpg

Basically, only http >> https part of the url needs to change.

What do I need to run in phpmyadmin to find all ref and change it?

Thank you.

Comments

Stefan Lehmann’s picture

UPDATE your_table
SET your_field = REPLACE(your_field, 'http://www.domain.com', 'https://www.domain.com')
WHERE your_field LIKE '%http://www.domain.com%';

That for every field you want to change. But be very, very careful, definitely take a backup etc.

Also be careful if a db field contains a serialized array. You can't do it for values like that. The replace would break that serialized array as the string length differs after the replace.

Also note there is this module here: https://www.drupal.org/project/pathologic .. which can fix links for changed URLs on the fly for you. It might be a safer approach, as you don't risk breaking everything. :-)

I like cookies!

sprite’s picture

All internal content in your drupal website should be using relative URLs so that changing to SSL or a different domain is painless.

There are even modules that can help with the process, which analyze and detect URLs that aren't properly formed.

spritefully yours
Technical assistance provided to the Drupal community on my own time ...
Thank yous appreciated ...

Stefan Lehmann’s picture

Good point.

I like cookies!

sprite’s picture

After you fix all the urls/paths in your website, try the following module, which manages making relative urls/paths within a Drupal website function correctly in all contexts.

https://www.drupal.org/project/pathologic

https://www.google.com/search?q=pathologic+module+site%3Adrupal.org

spritefully yours
Technical assistance provided to the Drupal community on my own time ...
Thank yous appreciated ...

sprite’s picture

Here is another of many similar modules that help maintain properly formed relative URLs in a Drupal website:

https://www.drupal.org/project/protocol_relative_urls

There are other modules also worth studying and testing for relevance to a particular need and/or site implementation.

spritefully yours
Technical assistance provided to the Drupal community on my own time ...
Thank yous appreciated ...

tepelena’s picture

Thank you all for the suggestions. Great tips for newcomers like me! :)

Ritvi’s picture

SSL certificate is done, but still the main site has the message, your connection to this site is not completely secure. even downloaded pathlogic and secure login. all the links are displaying "https" but the secure sign is not coming... Newbie to D8, please help

kari.kaariainen’s picture

Chrome DevTools - Audits - Best Practises may give you more precise info.

Utkarsh Harshit’s picture

Hi,

use REPLACE. and if there is a index on the field then the UPDATE can use them

UPDATE t
set url = REPLACE(url, 'http:', 'https:')
WHERE url like 'http:%';

this will only find row with 'http://example.com%'

UPDATE t
set url = REPLACE(url, 'http:', 'https:')
WHERE url like 'http://example.com%';

UPDATE t
set url = REPLACE(url, 'http://example.com', 'https://example.com')
WHERE url like 'http:%';

It may work.

junix33’s picture

This might help, I got this from this page - https://www.drupal.org/https-information

Redirect to HTTPS with settings.php

You can also force SSL and redirect to a domain with or without www in settings.php, the benefit is that it won't get overwritten after updating Drupal. Insert this at the top of settings.php, right after <?php:

// Force HTTPS
// PHP_SAPI command line (cli) check prevents drush commands from giving a
// "Drush command terminated abnormally due to an unrecoverable error"
if ( (!array_key_exists('HTTPS', $_SERVER)) && (PHP_SAPI !== 'cli') ) {
  header('HTTP/1.1 301 Moved Permanently');
  header('Location: https://example.org'. $_SERVER['REQUEST_URI']);
  exit();
}

// Remove www
if ($_SERVER['HTTP_HOST'] == 'www.example.org') {
  header('HTTP/1.0 301 Moved Permanently');
  header('Location: https://example.org'. $_SERVER['REQUEST_URI']);
  exit();
}
bisu_drupal’s picture

Yes it will work fine . but if you update your database  values then it  will be a bulletproof  :)   

kavbiswa’s picture

Thanks @Junix33 for the solution. This worked.

steveoriol’s picture

I just adapted the code for its use in copy / paste mode ;-)

// Force HTTPS
// PHP_SAPI command line (cli) check prevents drush commands from giving a
// "Drush command terminated abnormally due to an unrecoverable error"
if ( (!array_key_exists('HTTPS', $_SERVER)) && (PHP_SAPI !== 'cli') ) {
  header('HTTP/1.1 301 Moved Permanently');
  header('Location: https://'.$_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI']);
  exit();
}

Stève