Wednesday, March 30, 2016

Redirect url with dynamic server address in .htacess

To make it more dynamic lets use SERVER_NAME and REQUEST_URI instead of a static domain name. This is the way to redirect to HTTP non-www to HTTPS www htaccess.

RewriteEngine On

#we replace domain.com/$1 with %{SERVER_NAME}%{REQUEST_URI}.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) http://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

#here we dont use www as non www was already redirected to www.
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
 
 

No comments:

Post a Comment