Param Singh
Param Singh

Reputation: 21

Apache htaccess http redirect to https

I have setup my .htaccess file to redirect http://example.com to https://www.example.com. But http://www.example.com does not redirect to https.

I have my AWS route 53 setup at site.com and an alias of www.example.com pointing to example.com.

Here is my .htaccess file. I cannot for the life of me figure out how to redirect all to https://www.example.com.

RewriteBase /
RewriteEngine on

#WARNING: NEEDED FOR ONLINE VERSION - always have www in url
RewriteCond %{HTTP_HOST} !^www\. [NC]    
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,l]

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

RewriteCond %{HTTP:X-Forwarded-Proto} ^http$
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Upvotes: 1

Views: 261

Answers (2)

Narasimha Reddy
Narasimha Reddy

Reputation: 11

    Redirect HTTP to HTTPS on Apache Using .htaccess File

  1. For CentOS/RHEL users, ensure that your have the following line in httpd.conf (mod_rewrite support – enabled by default).
        LoadModule rewrite_module modules/mod_rewrite.so    
  2. Now you just need to edit or create .htaccess file in your domain root directory and add these lines to redirect http to https.
        RewriteEngine On 
        RewriteCond %{HTTPS}  !=on 
        RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]     
  3. Now, when a visitor types http://www.yourdomain.com the server will automatically redirect HTTP to HTTPS https://www.yourdomain.com.

    Redirect HTTP to HTTPS on Apache Virtual Host

  4. Additionally, to force all web traffic to use HTTPS, you can also configure your virtual host file. Normally, there are two important sections of a virtual host configurations if an SSL certificate is enabled; the first contains configurations for the non-secure port 80.

    The second is for the secure port 443. To redirect HTTP to HTTPS for all the pages of your website, first open the appropriate virtual host file. Then modify it by adding the configuration below.
        NameVirtualHost *:80
        <VirtualHost *:80>
        ServerName www.yourdomain.com
        Redirect / https://www.yourdomain.com
        </VirtualHost>
    
        <VirtualHost _default_:443>
        ServerName www.yourdomain.com
        DocumentRoot /usr/local/apache2/htdocs
        SSLEngine On
        ....
        ....
        </VirtualHost>
  5. Save and close the file, then restart the HTTP sever like this.
        $ sudo systemctl restart apache2     [Ubuntu/Debian]
        $ sudo systemctl restart httpd       [RHEL/CentOS]    

Note: While the <VirtualHost> is the most recommended solution because it is simpler and safer.

Upvotes: 1

Ashok
Ashok

Reputation: 870

This will force to redirect to https, add this in the bottom

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?!localhost$|127\.0\.0\.1$)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

Remove

RewriteCond %{HTTP:X-Forwarded-Proto} ^http$
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Upvotes: 0

Related Questions