5

This is a canonical question about redirecting from http to https in Apache

Related:

I have an Appache web server which serves both http://example.com/ and https://example.com/. I want to redirect all http requests to https. Currently I'm using this .htaccess rule to redirect http requests to https.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

It's working as expected for example.com but the same rule is not working for sub links, when I access existing links, like example.com/about it will still load in http, no redirection happens for existing links.

How can I make Apache redirect all http requests to https?

Augustin
  • 97
  • 2
  • 5

2 Answers2

10

You should configure Apache Virtualhosts to do the job. RewriteMod isn't the appropriate solution for this case and .htaccess isn't either.

In your httpd.conf or equivalent use the following lines accordingly your needs. Edit it to your domain and site.

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

<VirtualHost _default_:443>
   ServerName example.com
   DocumentRoot /usr/local/www/apache2/htdocs
   SSLEngine On

   ** Additional configurations here **

</VirtualHost>

Hope this clarifies the procedure.

Vinícius Ferrão
  • 5,400
  • 10
  • 52
  • 91
  • This is working perfectly on my server, but the issue I'm facing in a shared account, where I don't have access to httpd conf, how can I implement the same in .htaccess – Augustin Apr 17 '15 at 06:22
  • 3
    If you're on shared hosting, this site probably isn't for you. – AD7six Apr 17 '15 at 07:15
  • 1
    The `NameVirtualHost` line is no longer needed in current Apache versions. See https://serverfault.com/q/735233/214507 – kasperd Jan 22 '19 at 18:30
  • @Augustin If you are on a shared server then the Pro Webmasters stack is really the place to ask: https://webmasters.stackexchange.com/ – MrWhite Oct 16 '19 at 14:16
  • "`ServerName www.example.com example.com`" - It's not valid to have mutliple server names / arguments with the `ServerName` directive. Maybe you're thinking of the `ServerAlias` directive? – MrWhite Oct 16 '19 at 20:07
1

On shared hosting, when you don't have better options, you could modify your rewrite rule in the .htaccess:

RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

First, the RegEx at the beginning matches to all requests, including everything behind the domain.

Then, a HTTP Result code of 301 (moved permanently) is thrown back to the client together with the new URL. Most of the modern browsers remember the new URL, in this case the httpS, and redirect to the new url automatically the next time the user calls up the website.

I hope this helps, kind regards

Sebastian
  • 241
  • 3
  • 7
  • If http and https share the same directory, probably he will also need the mentioned `RewriteCond %{HTTPS} off`, otherwise it will loop forever :) – alphamikevictor Apr 17 '15 at 13:22
  • I tried above step, but it still not working for existing links like domain.com/about etc, but adding Redirect permanent / https://example.com/ in http.conf working in another server. – Augustin Apr 18 '15 at 06:49
  • Using Easy HTTPS Redirection plugin with wordpress solved my problem – Augustin Apr 19 '15 at 07:05
  • @Augustin "...with wordpress" - If you are using WordPress then the HTTP to HTTPS redirect would need to go at the top of the `.htaccess` file, _before_ the existing WordPress directives. The behaviour you have described in the question is consistent with having put the directives in the wrong place (ie. _after_ the WordPress front-controller). – MrWhite Oct 16 '19 at 14:22
  • "you could modify your rewrite rule in the .`htaccess`" - In terms of actually getting the redirect to function, the only significant take-away from this (over the directive in the question) is the `L` flag (which is not specifically addressed). The `L` flag is _required_ in order to prevent the request being further rewritten (by the WP front-controller that presumably _follows_) which would otherwise mangle the redirect. – MrWhite Oct 16 '19 at 19:15