Htaccess Basics - Redirecting to different domain, www, https or to a subfolder


.htaccess redirects can be done via several different methods, which all essentially work the same way. In this article we will cover a few different basic methods of .htaccess redirects, explaining what they do, how they work, and when you would want to use one over another.

There are two forms of redirects which are called 301 and 302 redirects. 301 Redirects are permanent redirects, which essentially tell Bots and Spiders (such as Google's Website Indexing Bot) that the redirect is a permanent displacement of the Website or Webpage, so that Google can index the new page or website accordingly. The 302 redirect is a Temporary Redirection, meaning that the redirect should be removed sometime in the future and that the normal website content should return and would be used when you do not want Google to update their indexing for your website.

With that bit out of the way, let's get to the actual redirect coding! 


The easiest redirect is simply redirecting one domain or webpage to another domain or webpage. This can be done by adding a single line of coding to your .htaccess file;

Redirect 301 / http://NewDomain.com/

Now let's break this down a bit!
The Redirect at the beginning tells Apache that you're going to be doing a redirect if the URL matches the clause.
The 301 (as we covered earlier) means that this redirect is going to be a permanent relocation.
The first / means that the redirect for "http://YourDomain.com/".
The last bit, http://NewDomain.com/, tells Apache to redirect requests to http://NewDomain.com/
All in all, this redirect would redirect any hits to "http://YourDomain.com/" to " http://NewDomain.com/"

We can make this redirect work only for certain URLs by appending the URL after the first /, as so;
Redirect 301 /RedirectMe http://NewDomain.com/Redirected
This redirect would redirect any hits to "http://YourDomain.com/RedirectMe" to " http://NewDomain.com/Redirected"

You would only want to use these types of redirect when redirecting an entire Domain or a specific Web Page or URL to another location (For Example "http://Domain.com/" to "http://NewDomain.com/" or "http://Domain.com/Page1" to "http://Domain.com/Page2").

You would not want to use this redirect to redirect a Domain to a Sub Folder of the same domain, as it will cause a redirect loop (For Example "http://Domain.com/" to "http://Domain.com/Sub-Folder/").
The next type of redirect is a bit more complicated, but is a much more powerful tool due to how heavily it can be customized.  These types of redirects are most commonly used when redirecting "Domain.com" to "www.Domain.com" or "http://Domain.com/" to "https://Domain.com/" for use with SSLs.


To redirect a domain to use the www. subdomain, you would want to use the following bit of coding in your .htaccess file;

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST} ^domain.com$

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


- The first line of coding turns the RewriteEngine used by Apache on. This should be on by default and should not be needed in most circumstances.
- The second line of coding is a little complicated. It essentially tells the Rewrite Rule what directory you're working from, as the rewrite rule is relative to the RewriteBase. In this case, and in most cases, the RewriteBase is the document root.
- The third line of coding is where the redirect really begins to take place. This line states the Rewrite Condition, which is essentially that the HTTP Host sent by the headers matches "domain.com". The $ at the end is a capture, taking anything after the / and storing it until the RewriteRule is executed.
- The final line of coding is a bit more complicated. This bit of coding essentially states that, when the Rewrite Conditions are met, that the page redirects to "http://www.domain.com/". The $1 at the very end is the capture from the RewriteCond and would automatically put in the URI from the initial URL onto the end of the new URL with the www sub-domain.

In practice this bit of coding would redirect the following;
http://domain.com to http://www.domain.com/

http://domain.com/ to http://www.domain.com/

http://domain.com/Redirected/ to http://www.domain.com/Redirected/

To do the opposite and redirect http://www.domain.com/ to http://domain.com/ you would want to use the following bit code;

RewriteEngine On

RewriteBase /

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

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


This coding is almost identical to the coding above, however we placed the 'www' sub-domain prefix on the RewriteCond line and removed it from the RewriteRule line, telling Apache to redirect http://www.domain.com/ to http://domain.com/.


Another very sought after redirect is an HTTPS redirect. This redirect is used when forcing HTTPS (SSL) on a Domain or Webpage. The code to do so would be the following;

RewriteEngine On

RewriteCond %{HTTPS} off

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

- The first line of coding turns the RewriteEngine used by Apache on. This should be on by default and should not be needed in most circumstances.
- The second line of coding is the Rewrite Condition, checking to make sure that 'HTTPS' is set to 'off' in the request headers. This is needed so that we do not redirect HTTPS URLs back to HTTPs again, creating a Redirect Loop.
- The third line of coding is again the effect of the redirect. If the RewriteCond was true (that HTTPS was off), then it rewrites the URL to "https://Domain.com/$URI"

In practice this bit of coding would redirect the following;
http://domain.com/ to https://domain.com/
http://domain.com/login/ to https://domain.com/login/
http://www.domain.com/ to https://domain.com/
http://www.domain.com/sub-folder/ to https://domain.com/sub-folder/



The final commonly used redirect is used to redirect your Domain to a Sub Folder under the Domain. This is typically done when people want their landing page to be a Sub Folder, such as "http://domain.com/store". The bit of coding would be;

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?Domain.com$
RewriteRule ^(/)?$ subfolder [L]


- The first line of coding turns on the Rewrite Engine for Apache.

- The second line of coding matches the HTTP Host sent in the headers to "Domain.com" or "www.Domain.com".
- The third line of coding is the sub folder that you'd like to redirect to. In this case it would redirect to /subfolder.

In practice this bit of coding would redirect the following;
http://domain.com/ to http://domain.com/subfolder/
http://www.domain.com/ to http://www.domain.com/subfolder/

Posted in
Last update:
2016-05-31 12:43
Average rating:0 (0 Votes)