How to redirect HTTP to HTTPS and domain1.com to domain2.com using cPanel or htaccess?

You can configure redirect rules to perform various redirects. Some can be set up using your hosting control panel, and others can be set up in your .htaccess file. Usually, redirects set up in the .htaccess file can be fine-tuned. The biggest benefit of using the .htaccess file is that it persists after a server migration.

 

Note:

  • We recommend you use the [.htaccess] file to create redirects.

    Redirects in the [.htaccess] file will be carried over when we upgrade or migrate servers.

  • Due to a known bug in cPanel on LiteSpeed-based servers, Wild Card redirects set in cPanel > Redirects do not function as intended.

 

Editing .Htaccess File


In cPanel, the [.htaccess] file remains hidden by default. You need to make it visible so you can edit it.

Make the file visible:

  1. Go to cPanel > File Manager.

  2. Click "Settings" from the top right corner.

  3. Check mark [*] "Show Hidden Files (dotfiles)".

  4. Click "Save".

  5. Done!

 

Edit the .htaccess file:

  1. Go to cPanel > File Manager.

  2. Navigate to the public_html directory. It is the root directory of your primary domain.

    If you are editing the .htaccess file of an addon domain or subdomain, navigate to the root directory for that domain/subdomain.
    You can view the root directories of all domains in cPanel > Domains.

  3. Select the .htaccess file and click "Edit".

  4. Click "Save Changes" after modifying the file.

  5. Done!

 

Redirect HTTP to HTTPS


This will create the following redirects:

  • http://yourdomain.com to https://yourdomain.com
  • http://www.yourdomain.com to https://www.yourdomain.com
  • http://yourdomain.com/page to https://yourdomain.com/page
  • http://www.yourdomain.com/page to https://www.yourdomain.com/page

cPanel:

  1. Go to cPanel > Domains.

  2. Set "Force HTTPS Redirect" to "On" next to your domain name.

  3. Done!

 

.htaccess:

Add the following redirect rules at the top (before all other rules) in the .htaccess file:

##### Start MechanicWeb Redirect Rule
##### Remove this section if not needed

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</IfModule>

##### End MechanicWeb Redirect Rule

 

Redirect HTTP to HTTPS with WWW


This will create the following redirects:

  • http://yourdomain.com to https://www.yourdomain.com
  • http://www.yourdomain.com to https://www.yourdomain.com
  • http://yourdomain.com/page to https://www.yourdomain.com/page
  • http://www.yourdomain.com/page to https://www.yourdomain.com/page
  • https://yourdomain.com to https://www.yourdomain.com
  • https://yourdomain.com/page to https://www.yourdomain.com/page

.htaccess:

Add the following redirect rules at the top (before all other rules) in the .htaccess file:

##### Start MechanicWeb Redirect Rule
##### Remove this section if not needed

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%2%{REQUEST_URI} [R=301,L]
</IfModule>

##### End MechanicWeb Redirect Rule

 

Redirect Domain1 to Domain2


This will create the following redirects:

  • https://yourdomain1.com to https://yourdomain2.com
  • https://www.yourdomain1.com to https://yourdomain2.com
  • https://yourdomain1.com/page to https://yourdomain2.com/page
  • https://www.yourdomain1.com/page to https://yourdomain2.com/page

cPanel:

  1. Go to cPanel > Redirects.

  2. Select "Permanent (301)" in "Type".

  3. Select your domain or subdomain in "https?://(www.)?".

  4. Enter https://yourdomain2.com in "Redirects to".

  5. Select [*] Redirect with or without www.

  6. Select [*] Wild Card Redirect.

  7. Done!

*To redirect to www.yourdomain2.com, replace yourdomain2.com with www.yourdomain2.com.

*Make sure to replace yourdomain2.com with your actual domain name.

Note:

cPanel currently has a known bug on LiteSpeed-based servers that creates a conflict between the redirect rule and existing rules.

Check the redirect after configuring it in cPanel. If it does not work, delete it from cPanel > Redirects and add the .htaccess rule.

 

.htaccess:

Add the following redirect rules at the top (before all other rules) in the .htaccess file:

##### Start MechanicWeb Redirect Rule
##### Remove this section if not needed

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain1.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.yourdomain1.com$
RewriteRule (.*)$ https://yourdomain2.com/$1 [R=301,L]
</IfModule>

##### End MechanicWeb Redirect Rule

*To redirect to www.yourdomain2.com, replace yourdomain2.com with www.yourdomain2.com

*Make sure to replace yourdomain1.com and yourdomain2.com with your actual domain names.

 

Redirect to Maintenance Page


This will create the following redirects:

  • https://yourdomain.com/ to https://yourdomain.com/maintenance.html
  • https://yourdomain.com/page to https://yourdomain.com/maintenance.html

Note:

*Make sure to replace yourdomain.com/maintenance.html with your actual maintenance page URL.

 

.htaccess:

Add the following redirect rules at the top (before all other rules) in the .htaccess file:

##### Start MechanicWeb Redirect Rule
##### Remove this section if not needed

<IfModule mod_rewrite.c>
ErrorDocument 503 /maintenance.html
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/maintenance.html
RewriteCond %{REMOTE_ADDR} !=123.123.123.123
RewriteRule ^.*$ - [R=503,L]
</IfModule>

##### End MechanicWeb Redirect Rule

 

  • 438 Users Found This Useful
Was this answer helpful?

Related Articles

How to change PHP configuration (memory, upload limit, and others) using the .htaccess file?

  PHP configuration (directives) like PHP memory limit, maximum upload size, post max size, and...

Error uploading large files (memory, HTTP or timeout error)

Change the following values in the [.htaccesss] file to upload large files:...

How to secure your htaccess file?

For security reasons, the [.htaccess] file contents should not be visible to the public. To...

How to enable Server Side Includes (SSI) using htaccess?

If you are using Server Side Includes SSI in your HTML files, and they are not working, you most...