Redirect website to https with .htaccess Print

  • https, redirekcija, .htaccess
  • 10

With this tutorial, we will redirect site from http to https connection (http://example.com to https://example.com).

At the top directory of the website (usually public_html), we will edit a .htaccess file and insert the following code.

# Comments in the .htaccess file begin with hash (#)
# We will turn on the redirection module

RewriteEngine on

# We will redirect to https

RewriteCond %{HTTPS} off
RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$1 [R=301,L]

# If the site's address does not start with www and we want that url start with www (redirect https://example.com at https://www.example.com).

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^/?(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]


# If the site address starts with www and you want to redirect it to the version of the site without www. We comment on the above two lines and uncomment the two lines below.

#RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
#RewriteRule ^/?(.*)$ https://%1/$1 [R=301,L]



Was this answer helpful?

« Back