Redirect http to https

Redirect http to https

Redirecting http to https is one of the important actions that must be done after preparing ssl . You may ask what is redirect? Redirect means the process by which the user is transferred to the new address of the site. This change of direction is sometimes permanent and sometimes temporary. Since after making changes to the site address or domain, redirecting users to the new address or domain is very necessary.

In the rest of this article, we will examine the method of redirecting http to https and vice versa. We will also introduce you all the pieces of code that you need to redirect different parts of the site.

Redirect http to https in htaccess

There is no need to use special plugins to redirect http to https in htaccess because the desired changes are supposed to be made by changing the site’s htaccess file. For this, you must first go to your host and then click on the file manager option to access the file management section. After that, you have to click on the setting option and check the Show Hidden Files (dotfiles) option so that the htaccess file is visible to you.

Then, by entering the list of files, you should go to the public_html folder and apply the necessary changes to the htaccess file. To change this file, you must click on the edit option and enter the Zira code.

RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect https with www (https://www to without www

Another point that is important in redirecting http to https is redirecting https with www (https://www to without www), for which you need to go through the following steps:

  • Refer to the control panel
  • Select the File Manager option
  • Enter the public_html folder
  • Click on the htaccess option
  • Right click and select edit option
  • Enter the following code snippet

RewriteEngine On

RewriteCond %{HTTPS} off [OR]

RewriteCond %{HTTP_HOST} ^www. [NC]

RewriteRule ^ https://okex.ir%{REQUEST_URI} [L,NE,R=301]

 

Redirect https to http in htaccess

In order to redirect from the secure HTTPS protocol to the HTTP protocol, you must follow the following path:

  • Login to the host
  • Click on the File Manager option
  • Go to the Public_html folder and select the htaccess option
  • Right click on the htaccess file
  • Select the edit option
  • Write the code below

# Redirect HTTPS to HTTP

RewriteCond %{HTTP:X-Forwarded-Proto} =https

RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect site address without www to address with www

If you want the address of your desired site to be opened for users only with www, you must enter the following piece of code in the .htaccess file. Then you need to save these settings by clicking the Save Changes option.

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

Important note: the example.com part is for the domain name, so you should enter the domain address of the desired site instead.

Redirect site address with www to address without www

Among the path changes that you may need in http to https redirect, is the site address redirect. If you want the address of your site to be opened for the user without www, as in the previous section, you must enter the following code in the .htaccess file. Don’t forget to put the domain address of the desired site instead of example.com.

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www.example.com [NC]

RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]

Redirect http to https except a directory or folder

To redirect http to https except for a directory or folder, you must enter the following code in the edit section and put the name of your desired directory instead of demo.

<IfModule mod_rewrite.c>

RewriteCond %{HTTPS} on

RewriteRule ^%{HTTP_HOST}/demo(.*) http://%{HTTP_HOST}/demo/$1 [R=301,L]

RewriteRule ^index\.php$ – [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>

Redirect only one folder of the site on the https platform

If you intend to redirect http to https only one folder of the site on the https platform, just put the following piece of code in the edit section. Just keep in mind that you should put the name of the desired folder instead of folder. Also, replace www.example.com with the domain name of the desired site.

RewriteEngine On

RewriteCond %{SERVER_PORT} 80

RewriteCond %{REQUEST_URI} folder

RewriteRule ^(.*)$ https://www.example.com/folder/$1 [R,L]

Redirect subdomain on main domain

To redirect the subdomain on the main domain, you must first enter the subdomain folder and remove the hidden files from the hidden mode by going to Settings > and activating the Show Hidden Files option. After this step in the .htaccess file

You must use the following code. Just remember to put your subdomain name instead of Your SubDomain and the main domain name in Example.com.

#Options+FollowSymlinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^sub.domain.com$ [OR]

RewriteCond %{HTTP_HOST} ^www.sub.domain.com$

RewriteRule (.*)$ http://domain.com/$1 [R=301,L]

Redirect a file in htaccess

When redirecting http to https, in order to redirect a file in htaccess, you must first remove this file from hidden mode. In fact, to do this, you must go through the following steps:

  • Enter the public_html section
  • Click on the settings option
  • Tick ​​show hidden files
  • Enter the code below

RedirectMatch 301 /oldurl /newurl

Redirect all addresses of a domain to a specific address

In the process of redirecting http to https by entering the following piece of code, a permanent redirect occurs and all addresses of a domain are redirected to a specific address. Usually, after making these changes, when the user tries to enter an old URL, this change will redirect him to a specific address.

RedirectMatch 301 / /newurl

302 redirect in .htaccess

In the http to https redirect process, the 302 redirect is a temporary change because it redirects search engines and users to new locations for a limited time. In general, it should be said that redirect 302 in htaccess. It is considered a very simple redirection because it can be done with the help of a meta tag or in JavaScript and requires less time for changes than 301 redirect. The following code is considered as the main code fragment of redirect 302.

Redirect 302 / http://domain.com/

Redirect old domain to new domain

In order to redirect the old domain to the new domain, you need to go to CPanel > public_html > htaccess file. and put the following code in its edit section. Just don’t forget to put the old domain name in the domain.ir section and put the new domain name instead of domain.com.

RewriteEngine on

RewriteCond %{HTTP_HOST} ^ domain.ir [NC,OR]

RewriteCond %{HTTP_HOST} ^www.domain.ir [NC]

RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301,NC]

Redirect one page to another page

When redirecting http to https, you need to use the following code to redirect one page to another. Just note that instead of old-index.html, enter the name of the previous page and the name of the new page in the mynewwebsite field.

Redirect /old-index.html http://www.mynewwebsite.com/foldername/new-index.html

Redirect parked domain to subdomain

To redirect the parked domain to the subdomain, you must enter the path of the code below and insert the name of the parked domain instead of parkdomain.com. Also, instead of main domain/subdomain, put the address of the subdomain.

RewriteEngine on

RewriteBase /

RewriteCond %{HTTP_HOST} ^(www.)?parkdomain.com$ [NC]

RewriteRule ^(.*)$ http://main domain/subdomain [L]

Redirect main domain to subdomain

To redirect the main domain to the subdomain, you must enter the following code in the edit section. At the same time, you must replace the domain name and its extension with domain and .com and put the name of the subdomain in the sub.domain.com field.

Options +SymLinksIfOwnerMatch

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]

RewriteRule ^ http://sub.domain.com[R,L ]

Redirect webmail

To change webmail.domian.com to domain.com/webmail, you must enter the following code:

RewriteEngine on

RewriteBase /

RewriteCond %{HTTP_HOST} ^(www.)?webmail.domain.com/$ [NC]

RewriteRule ^(.*)$ http:// domain.com/webmail/ [L]

It is necessary to remember that instead of domain.com, you must put your desired domain name.

Redirect subdomain to ip with port

If you put the name of the parked domain instead of parkdomain.com in the code below and put the address of the subdomain in the main domain/subdomain section, the subdomain will be redirected to the IP with the port.

RewriteEngine on

RewriteCond %{HTTP_HOST} ^subsonic.mydomain.com$ [NC]

RewriteRule ^(.*)$ http://ip:4040/$1 [R=301]

Redirect all server traffic to https

By putting the following code in the edit section of the htaccess file, all traffic will be redirected to https://www.example.com .

RewriteEngine On

RewriteCond %{SERVER_PORT} 80

RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

Redirect a specific extension of the site pages to another extension

By writing the following code, pages like http://domain.com/Page.html will be redirected to http://domain.com/Page.php. In other words, it can be said that the specific extension of the site pages changes the path to another extension. Of course, it should be noted that this change of path is a permanent change and the path of the site changes permanently. This method is also useful for SEO purposes because search engines understand that the site’s path has changed forever.

RedirectMatch 301 (.*)\.html$ http://domain.com$1.php

Domain Redirect in cPanel

To redirect the domain in cPanel, you must follow the steps below.

  • First, enter cPanel
  • After that, go to the “Domain” menu and click on the “Redirects” link.
  • After that, you will see a window that has different fields in which you must enter information.
  1. Usually, the first field you see is called “Type” where you have to choose the type of redirection. In fact, you have to say that you intend to redirect your site permanently (301) or temporarily (302).
  2. You must enter your domain name in the second field.
  3. The third field is also for redirecting specific files.
  4. In the Redirects to field, enter the name of the domain you want to be redirected to.
  5. After completing the fields at the bottom of the page, there is a section called “Redirection” through which you must choose the type of management of www redirects.
  • After applying all the changes, just click on the Add option to apply all the included changes.

Online Htaccess code generator

Among the actions that can help keep your site in Google is changing the path or redirecting files and pages. If you need htaccess codes to redirect http to https and you don’t know about them. You can get the special codes instantly by visiting the following two websites.

support hosting100