0

I need to change an entire PHP-based website from http:// to https://. The SSL certificate has already been installed and shows validity.

Now, the website has many many subdirectories, shops, newsletters etc., but stems from one major directory.

Is there either a tool or a methodology I can do this under Linux recursively, i. e. incorporating all various sub-directories in my search and automatically exchange http:// to https://? Is there a way not only to do the exchange but also to save the changed files automatically?

Maybe a stupid question, but I'd appreciate your help a lot so as to prevent myself from going through every single PHP file in every single directory.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
mtjmohr
  • 99
  • 9
  • How about just creating a `htaccess` rule that forwards all http requests to https? – Daniel Feb 10 '18 at 10:11
  • Yes, thank you, Daniel, this has already been done, but as long as there are hardcoded http:// calls in the PHP files, it will always show you the (i) sign on the left of your URL. This is the issue for me here. – mtjmohr Feb 10 '18 at 18:25

1 Answers1

1

The sed command has an in-place option which can be helpful in executing your change. For example

sed -i 's/original/new/g' file.txt

In your case this may work

sed -i 's/http:\/\//https:\/\//g' ./*.php

I would recommend a backup before you try this since the sed command -i option may work differently on your system.

Here is a reference with more information.

  • Thank you very much, Stephen, I think this is the one I will check out right now. I will tell you about its success. – mtjmohr Feb 10 '18 at 18:26
  • Hello, Stephen, with a slight modification to ... sed -e 's/http:\/\//https:\/\//g' -i.bak $(find . -type f -name '*.php') ... the job could be finished excellently. Many thanks again Markus – mtjmohr Feb 10 '18 at 22:59
  • a less cluttered command is find . -name "*.php" -exec sed -i 's|http://|https://|g' {} \; This will find all files with php extension in the present directory where this command is run and within subdirectories and replace http:// with https:// – Ravi Kumar CH Jul 15 '21 at 09:55