Tag Archives: htaccess tricks

[SOLVED] Force SSL and WWW on Sub Domain with .htaccess

I had someone today who was having some issues with their financial site and the SSL cert so I solved the main issue but noticed that visitors could navigate to https://www.{subdomain}.{domain}.com (with www).

Ideally, to keep things clean, they need to go to https://{subdomain}.{domain}.com (without www).

So after scrambling around for a bit trying various methods I settled on the solution below which you can place in your root .htaccess file and this will detect requests trying to access www on the subdomain and redirect them to the sub domain without www but maintaining/forcing SSL.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{ENV:HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

Basically with this script, all roads lead to SSL on subdomain.

Move Blog from Subdirectory to New Domain using htaccess

Lets say you have wordpress blog sitting in the following domain and directory:


But now, you want to move the blog to:


However, and here is the caveat, you want to maintain all your search engine pages for the blog because currently google is looking at and listing all the pages on domainA.com/blog and you need to tell google where to look for the moved pages.

Basically this is what you want to do:

OLD SITE: http://www.domainA.com/blog/cat-stories
NEW SITE: http://www.domainB.com/cat-stories

All you do is place the code below into a .htaccess file on http://www.domainA.com/blog/ and all your url’s will move to the new domain in a nice seo friendly manner using 301 redirects.

RewriteEngine on
RewriteBase /
RewriteRule ^blog/(.*) http://www.domainB.com/$1 [R=301,L]

This code basically tells everyone that the pages have been moved from site A to Site B and it will keep all the url’s in place meaning you don’t have to make individual 301 redirects.