3

I want to redirect all of my HTTP requests to my HTTPS (SSL) protocol. Please guys this is not a duplicate. I have already tried every question found on SO which indicate such question but none of them worked. I tried everything I found on the internet but still cannot figure out what the problem is. May be somewhere something has to be enabled or something like that.

I want to redirect from my httpd.conf file not from .htaccess because this is what suggested by Apache Org. My current Redirection Rule is

<VirtualHost *:80> 
    ServerName www.example.com
    Redirect / https://www.example.com/
</VirtualHost > 
<VirtualHost *:443>
    ServerName www.example.com
</VirtualHost >

I am making all of these changes in my httpd.conf file located in /etc/httpd/conf/httpd.conf

After every change, I also restart my server with following /etc/init.d/httpd restart or service httpd restart

I also tried following

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/%$1 [R,L]

But still when I visit my site like www.example.com, I am not redirected to https://www.example.com.

I have a VPS Server so has enough access to my machine to enable or disable something.

Airy
  • 5,484
  • 7
  • 53
  • 78
  • have you tried everything in [this question](http://stackoverflow.com/questions/16200501/http-to-https-apache-redirection)? (and make sure you restart apache) – DevDonkey Mar 28 '16 at 07:45
  • @DevDonkey Yes Sir. I already tried everything in that question. Infact I learned from that how to redirect from Http to Https – Airy Mar 28 '16 at 07:46

2 Answers2

4

Try with:

RewriteEngine on 
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R] 
Croises
  • 18,570
  • 4
  • 30
  • 47
0

This working for me:

 # Force SSL
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP:X-Forwarded-Proto} =http
  RewriteCond %{HTTP_HOST} ^example.com
  RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]