How do I setup a domain redirect using htaccess?

You can setup a domain redirect in a couple of ways. We’ll outline these below:

How do I setup a domain redirect using .htaccess file?

A .htaccess file is an important Apache configuration file usually resides within the public_html folder of your site, it’s a way to add specific rules on a per-hostname basis. A .htaccess rule allows for further configuration of your redirect should you need a more specific redirect. Below are some common examples you may need to add.

Redirect your website to a new domain

To redirect your website to another domain using a .htaccess rule you’d want to add the following rule to your .htaccess file and then save the file:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldsite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.com [NC]
RewriteRule ^(.*)$ http://newsite.com/$1 [L,R=301,NC]

Force www. instead of non-www.

You could achieve this by editing the site and home URL (if your site is WordPress), but it can also be achieved by using the following rule:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [L,R=301,NC]

Force HTTPS

You can use the Force HTTPS tool from within the control panel to always enable https://, however you can also add the following rule:

RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

  • 18 کاربر این را مفید یافتند
آیا این پاسخ به شما کمک کرد؟

مقالات مربوطه

What details should I use for a WordPress SMTP plugin?

  The best SMTP details to use when configuring an SMTP plugin within WordPress when you're...

How to Optimize WordPress for Better Performance

In this tutorial we will give you some tips on how to improve the speed of your WordPress...

Which PHP functions are disabled on your WordPress platform?

Our WordPress platform has been specifically designed to offer the best security, performance and...

Can you update the version of cURL on your webservers?

Occasionally, applications in your webspace may run checks to help ensure your website is secure....

Can I enable GZIP Web Server Compression?

Gzip is a great of compressing files for faster network transfers - helping them load faster, so...