miercuri, 23 ianuarie 2019

Apache Redirect Combo

This is a simple and a well known setup for Apache servers. But this still can be a challenge when having to do a quick setup with a combo between two redirects:

  • one redirect from port 80 to port 443 (from plain HTTP to HTTPS);
  • one redirect from https://www.yoursite.com to https://yoursite.com without sacrificing the HTTPS aspect;
So here is the snippet.


# Redirect http to https
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://yoursite.com/$1 [R=301,L]

# Redirect www to non-www/https
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com
RewriteRule (.*) https://yoursite.com/$1 [R=301,L]