I want to enable SSL for my main website and sub-domain websites. For SEO reasons, I need to redirect HTTP requests to HTTPS requests. I think I should do that using a wild card redirect in my .htaccess file, but, I already have a wild card redirect in my htaccess file as follows:

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

As you can see, that will redirect Non-WWW version of my site to WWW version. I need to know how to utilize Both Redirects for my websites and domain.

  • 1
    You wouldn't be able to do this on the .htaccess level unless your Domain DNS is setup to support a wildcard. Basically standard DNS will support CNAME and A Record. www = cname, so unless your dns supports cname = * then you can't do that, it'd only work for created CNAMES. Also, what is the purpose of this? seems like your trying to fix a wheel that isn't broken. Commented Oct 6, 2014 at 16:37
  • can you please be more clear about that explanation? i am using cloudflare's name servers. what i'm gonna do is perfectly natural. the redirects including (non-www to www and http to https) are both for the sake of SEO matters.
    – user38723
    Commented Oct 6, 2014 at 16:49

2 Answers 2


Looks like you didn't "escape" the slashes in your directive. Putting backslashes before any / . or : should make it work. Also adding the ^ ("begins with") and $ ("ends with") on the wildcard helps. Here is what we use:

Standard Domain: Perhaps there is a consolidated way, but this snippet should work for a standard domain. Change the target of the first RewriteRule to https if you need all to https mode:

RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.example\.com\/$1" [R=301,L]

RewriteCond %{HTTPS_HOST} ^example\.com$
RewriteRule ^(.*)$ "https\:\/\/www\.example\.com\/$1" [R=301,L]

Addon Domain: If you are trying to do this with an "addon domain", and want to redirect the subdomain utility scheme, this would do that. Again, change the target of first directive to https and it should force SSL:

RewriteCond %{HTTP_HOST} ^addondomain\.example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.addondomain\.example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^addondomain\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.addondomain\.com\/$1" [R=301,L]

RewriteCond %{HTTPS_HOST} ^addondomain\.example\.com$ [OR]
RewriteCond %{HTTPS_HOST} ^www\.addondomain\.example\.com$ [OR]
RewriteCond %{HTTPS_HOST} ^addondomain\.com$
RewriteRule ^(.*)$ "https\:\/\/www\.addondomain\.com\/$1" [R=301,L]

Hope that helps!

  • The first one worked perfect for me. thank you! i really need to have some basics of http requests. would you please explain what exactly those 2 directives do? because when i look at the redirects page of Cpanel i only see one redirection. and the second example is brilliant but i don't use Add-on domains, instead i need to know how to set this up for a website ( in my case a wordpress one ) located in a sub-folder. I've read many articles about that process but i want to know whether it is possible through Htaccess, too.
    – user38723
    Commented Oct 6, 2014 at 19:47
  • I wouldnt trust what cPanel says about redirects you make manually in HT. Basically it looks for any derivative and pushes URL to www mode, then puts the original URi at the end of that. As far as redirecting wordpress goes, it should be possible with HT. How are you pointing your domain relative to the wordpress install folder? Do you use an A record or is it sitting right in folder? And do you want to mask the install folder in WP URL?
    – dhaupin
    Commented Oct 6, 2014 at 19:55
  • well i'm not using A record for websites in sub-folders. each site is in its own folder. if your meaning by "masking the install folder" is that to change the install folder and yet keep the website accessible on its address then it will be good to do that, if that won't make it complicated to redirect. the reason why i need to redirect wordpress site through HT is that that way all the files (js, css, images etc) will also load through https.
    – user38723
    Commented Oct 6, 2014 at 20:08
  • 2
    HTTPS_HOST does not exist.
    – freedev
    Commented May 12, 2017 at 8:57
  • 2
    "Looks like you didnt escape the slashes..." - Slashes, dots and colons don't need to be escaped in the RewriteRule substitution - it's an ordinary string. But slashes and colons don't need to be escaped in mod-rewrite regex either. (cPanel escapes everything - but that's just cPanel.) "Also adding the ^ and $ on the wildcard helps." - that's entirely optional, it doesn't "help" the regex. And as freedev suggests, HTTPS_HOST is either very non-standard, or a typo? (TBH, it looks like a typo, the standard vars are HTTP_HOST and HTTPS.)
    – MrWhite
    Commented Aug 22, 2017 at 17:29

A few assumptions:

  1. Everything should be redirected to HTTPS
  2. Only the apex domain should be redirected to the www subdomain. Subdomains are not redirected to the www sub-subdomain. ie. subdomain.example.com is not redirected to www.subdomain.example.com.
  3. Your SSL cert covers the main subdomain, www subdomain and all subdomains.
  4. Your SSL cert is installed on the application server, not a front-end proxy.

Try something like the following using mod_rewrite in the root .htaccess file:

RewriteEngine On

# Redirect the apex domain to www subdomain (ensure HTTPS)
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]

# Redirect to HTTPS if not already
RewriteCond %{HTTPS} !on
RewriteRule (.*) Https://%{HTTP_HOST}/$1 [R=301,L]

NB: To help with development it is often easier to test with 302 (temporary) redirects, which are not cached by the browser. 301 (permanent) redirects are cached hard by the browser (by default), so can make testing problematic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.