seaBass
seaBass

Reputation: 607

Redirect using .htaccess from http to https

I have two domains on the same server. One domain is for production and the other is for testing content before it is copied over to the production. I am trying to create a rewrite rule that redirects http to https. How can I write it so that domain name is not hard coded? The reason I need this is when I copy the tested content to the production server I don't want have to change the .htaccess file. Here is what I have but it doesn't work.

 # Prevent SSL cert warnings
 <IfModule mod_rewrite.c>
  RewriteCond %{SERVER_PORT} !^443
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
 </IfModule>

Upvotes: 0

Views: 138

Answers (2)

Jeff
Jeff

Reputation: 9702

RewriteEngine On

# This checks to make sure the connection is not already HTTPS
RewriteCond %{HTTPS} !=on


RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

Upvotes: 0

anubhava
anubhava

Reputation: 785971

You're pretty close in your attempt.

You can try this rule:

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Upvotes: 1

Related Questions