Software

Convert your sitelinks to https with `sed`

By Igor Moiseev · · 2 min read ·

I wrote a small post before on how to find and substitute the string in all files with sed command in GNU/Linux. Now I’d like to show a real use case.

You’ve bought a new SSL certificate and configured your web server. After you fire it up, you may make an unfortunate discovery when you open the browser on your brand new https://example.com, that it says “Some parts of this page are not secure”.

So what is insecure? Simply, you may have used insecure content like images or external JS libraries loaded from CDN.

To help with this, Linux has a small and powerful command to find and substitute the old_phrase with the new_phrase in all files and directories recursively - it is the sed command on GNU/Linux

find . -type f -print0 | xargs -0 sed -i 's/old_phrase/new_phrase/g'

Attention! The previous command finds files also in the hidden folders and if you’re working with Subversion or GIT you’d like to skip them. The following keys -not -path '*/\.*' do the trick

find . -not -path '*/\.*' -type f -print0 | xargs -0 sed -i 's/http:\/\//https:\/\//g'

After this command your links like

<img src="http://example.com/dot.png">

will be converted to

<img src="https://example.com/dot.png">

Happy coding!