Mobile app version of vmapp.org
Login or Join
Carla537

: Wild card redirection for HTTPS and Non-www version 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 and

@Carla537

Posted in: #Htaccess #Https

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 and i think i should do that using wild card redirect in htaccess file. But, i already HAVE a wild card redirect in my htaccess file as follow

RewriteCond %{HTTP_HOST} ^mscaspian.com$
RewriteRule (.*) www.mscaspian.com/ [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

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Carla537

2 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

A few assumptions:


Everything should be redirected to HTTPS
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 subdomain.example.com. Your SSL cert covers the main subdomain, www subdomain and all subdomains.
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 (.*) www.%{HTTP_HOST}/ [R=301,L]

# Redirect to HTTPS if not already
RewriteCond %{HTTPS} !on
RewriteRule (.*) %{HTTP_HOST}/ [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.

10% popularity Vote Up Vote Down


 

@Si4351233

Looks like you didnt "escape" the slashes in your directive. Putting backslashes before any / . or : should make it work. Also adding the ^ and $ on the wildcard helps. Heres 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 rewrite to https if you need all to https mode:

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

RewriteCond %{HTTPS_HOST} ^example.com$
RewriteRule ^(.*)$ "https://www.example.com/" [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/" [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/" [R=301,L]


Hope that helps!

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme