url without www and http to https in drupal 8/9

5
(1)

On the Internet there are many options for implementing redirection to the secure protocol ssl (https) for Drupal 8/9, but 80% of them did not suit me, for various reasons, some did not work, others created cyclic redirection, and I decided to read the documentation, and as it turned out everything is very simple, as they say the best code is a short code, that’s how it turned out, let’s get started.

Redirect url from http to https for Drupal 8 / 9

At the root of the site there is a .htaccess file, tear it off with notepad + and look for the code:

RewriteRule ^ - [E=protossl]
 RewriteCond %{HTTPS} on
 RewriteRule ^ - [E=protossl:s]

Comment on it, put the character # at the beginning of each line or delete these lines of code, and insert after it:

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Save the file, clear your browser cookies, and reload the page with ctr + F5, if you have chrome, ready.

Note: that the code is not my regular expression, which reduces the load on the CPU.

Remove the www prefix from the address for Drupal 8 / 9

Many webmasters do not take into account that if there is no redirection from www.example.com to examle.com, the site will be available at two addresses, which means that for search engines these are two different sites, so it turns out that you will have duplicate content. , and this can lead to filters and underestimation of search engine rankings. Let’s do a redirect from www.

Open the .htaccess file in the notepad++ find the lines with the code:

 # To redirect all users to access the site WITHOUT the 'www.' prefix,
  # (http://www.example.com/foo will be redirected to http://example.com/foo)
  # uncomment the following:
  # RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
  # RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]

And uncomment two lines:

# To redirect all users to access the site WITHOUT the 'www.' prefix,
  # (http://www.example.com/foo will be redirected to http://example.com/foo)
  # uncomment the following:
   RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
   RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]

Now we have the https protocol on the site and there is no www prefix, I say two hares with one shot. If you have questions, ask them in the comments.

Similar Posts:

1,564

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top