This is the ultimate solution for redirecting all URLs from HTTP to HTTPS in Drupal.
HTTPS is the latest encryption standard for the web. It’s more secure than HTTP, which was once the standard. Most browsers currently support HTTPS, and it’s becoming more popular as more websites switch over. Drupal is no exception: we recommend that all URLs be redirected to HTTPS. This will make your website more secure and protect your data from being intercepted or stolen.
You know that a while ago Google made a statement, that all sites should use secure HTTPS URLs. That they will even give the advantage to sites that use HTTPS in search engine results.
WordPress has a nifty superb small module for that. You just enable it and voila.
But Drupal is different. I used to add my own code to the .htaccess code to redirect all URLs from HTTP to HTTPS in Drupal.
Drupal 7
In drupal 7 you can use this little module called HTACCESS MODULE.
You download it and then enable it.
Then you go under the admin menu to Configuration ----- System ------Htaccess.
Here you select the version that you prefer...www or without the www version...
And also you can select to force HTTPS in the settings. Below.
Here is the screenshot.
The next step is to generate the .htaccess file, just click on the second button generate....here is the screenshot.
And the last step is to deploy the .htaccess you just generated...
Here is more info about HTTPS preferred sites directly from Google
Drupal 8 / 9 .HTACCESS REDIRECT
UPDATE 2022
In Drupal 8/9 you can´t use the module that I linked above, it is only compatible with Drupal 7.
It is easier to just do the following step anyways.
You need FTP program like Filezilla. Then you edit the .htaccess file that is in the main directory of your drupal installation. If you use a composer for managing your Drupal site on the other hand then it should be in /web folder.
When you open the file you just add the following code.
I have two variations that work for my host. I am on Bluehost for this website. So you can test each of them and see if it works.
Remember these two codes redirect all traffic from HTTP to HTTPS and also non-WWW to WWW
HTTPS CODE VARIATION 1
HTTPS CODE VARIATION 2
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Drupal 8 / 9 Cloudflare redirect
- Speed: Cloudflare helps to improve website loading times by caching and redistributing content across multiple servers around the world
- Protection: Cloudflare helps to protect websites from hacker attacks that might attempt to exploit vulnerabilities in the website code
- Security: Cloudflare also protects websites against malicious intrusion by tracking user activity and providing defensive measures such as anti-spam and antivirus services, it prevents spam comments, spam registrations, and more
- Price: CloudFlare is free. I use a free plan, that covers everything you will need. There are paid plans for more advanced users, but in general, it is free
Steps to add your Drupal site to Cloudflare.
First of all, I recommend you download fresh .htaccess file from drupal.org for your drupal version. You must not use any redirects inside....including NON-WWW to WWW redirects inside .htaccess or WWW to NON-WWW version. That will cause multiple redirections after you add your site to Cloudflare.
You should not use any code that I gave you above, these are only if you host your site on regular hosting without Cloudflare.
When you add your site to Cloudflare, it will ask you to confirm a few parameters. You should enable REDIRECT ALL TRAFFIC TO HTTPS. which will automatically redirect everything from HTTP to HTTPS in your Drupal site.
You can also find this setting later on under SSL/TLS ---- EDGE CERTIFICATES
Also, I recommend you do a general NON-WWW to WWW URL redirect inside Cloudflare, just to avoid any kind of Google duplicate URL issues and here also.
The next step is to go to RULES. Here is the screenshot, add a new rule, and choose the options that you see in the picture below. Replace the URL with your site URL.
You just have to do two steps:
First, add your site to the URL box
seoandwebdesign.com/* be careful to add /* at the end and use your own website URL of course
Select forwarding URL and 301 redirect
Under entering the destination URL use
https://www.seoandwebdesign.com/$1 be careful to put /$1 at the end of the URL and use your own website URL.
That is pretty much it, your site should work perfectly with HTTPS and WWW redirect.
Add new comment