Skip to content

Instantly share code, notes, and snippets.

@adrianrodriguez
Last active July 7, 2016 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianrodriguez/e6f5ac6452bddb99a2dac39a6c5435ab to your computer and use it in GitHub Desktop.
Save adrianrodriguez/e6f5ac6452bddb99a2dac39a6c5435ab to your computer and use it in GitHub Desktop.
A problem finally solved. Redirecting non HTTP and non WWW to www and https for MAIN DOMAIN ONLY.
# We had a specific need for a client that wanted HTTPS for one domain but not subdomains
# that also ran a lot of sites locally. So we ran into an issue after launching without the www prefix.
# For days I looked for a solution and tried to get things working, with hours spent attempting many different ones.
# After so long and taking some time to work on another project I stumbled upon this article: https://www.siteground.com/kb/how_to_redirect_nonwww_urls_to_www/
# After sifting through the comments I found the first chunk of code below that did everything for the main domain, while keeping what I had below for the subdomains to redirect from HTTPS
# Also be sure the main blog is updated to http://www.domain.com in the wp_options table wherever applicable.
# for main domain
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^domain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
# for sub domain
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com [NC]
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment