sureshprasanna70
sureshprasanna70

Reputation: 1045

Redirect to https a rails application reverse proxied with apache using .htaccess

I am running a simple rails application on port 3000. I have reverse proxied it to apache port 80 with mod_rewrite enabled. When I access via http and https the application works fine. Now I would like to redirect all http request to https. I already have a .htaccess file under public folder of the rails application but it doesn't seem to work.

RewriteCond %{SERVER_PORT} 80 [OR,NC]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]

Domain.com points to my domain. Also I don't want my subdomains to be redirected to https. How do I achieve it?

Upvotes: 0

Views: 196

Answers (2)

Olaf Dietsche
Olaf Dietsche

Reputation: 74018

To check if https is enabled, you can use RewriteCond with the HTTPS variable

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L]

Upvotes: 0

Amit Verma
Amit Verma

Reputation: 41209

Remove the OR condition and try:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]

Upvotes: 1

Related Questions