Http To Https Apache



Skip to end of metadataGo to start of metadata

HTTP to HTTPS

Redirect Http To Https Apache Htaccess

Redirect http to https apache virtual host

Scenario :

You want to force people coming to your site to use HTTPS. Either for the entire site or a small sub-section of it.

Https

Apache HTTP Server Documentation¶ The documentation is available is several formats. Downloadable formats including Windows Help format and offline-browsable html are available from our distribution mirrors. Online browsable documentation is also available: Version 2.4. Version 2.2 (Historical) Version 2.0 (Historical) Version 1.3 (Historical). Redirect HTTP to HTTPS using.htaccess.htaccess is a configuration file on a per-directory basis for the Apache webserver. This file can be used to define how Apache serves files from the directory where the file is placed and to enable/disable additional features. The Apache Incubator is the primary entry path into The Apache Software Foundation for projects and codebases wishing to become part of the Foundation’s efforts. All code donations from external organisations and existing external projects seeking to join the Apache.

  • Note*

Using mod_rewrite to do this isn't the recommended behavior. See RedirectSSL

Fix :

Entire site (.htaccess) :

Note: While the rules you need are the same as above (because the rule above doesn't depend on any of the quirks of rewrite in .htaccess), you will need to ensure that you place this in a .htaccess file in the root of the site you want to apply it against, and to make sure you have the appropriate AllowOverride configuration in your httpd.conf

Specific Directory

Either put the above solution in a .htaccess file in the directory to be affected, or put the URI prefix in the regex itself.

Webmasters and students may want to learn how to redirect an old domain to a new one or one sub-domain to another or HTTP to HTTPS… For example, with Apache2 redirect modules, one can redirect http//example.net to http://example.com or http://www.example.com to http://example.com or http://example.com to https://example.com

This brief tutorial is going to show students and new users how to set up Apache2 directs easily… the method below can also be used with switching your HTTP domain to HTTPS…

When you’re ready to configure Apache2 directs, continue below:

Step 1: Install / Configure Apache2 HTTPS

To configure Apache2 to redirect all traffic to HTTPS, you must configure and enable its SSL modules.. The post below shows you how to install and configure Apache2 to communicate over HTTPS… however, both HTTP and HTTPS are enabled… the method below will show you how to redirect all traffic to the server to use HTTPS only..

Step 2: Redirect Apache2 HTTP to HTTPS

Redirect

Http To Https Apache Redirect

Our post above set up Apache2 to communicate over both HTTP and HTTPS… however, in today’s environment, it’s recommended to choose one type of protocol and redirect the other to it..

For example, you should redirect all HTTP (80) to HTTPS (443). To do that, add the highlighted portion of the code to your Apache2 site configuration file…

Run the commands below to open Apache2 default site configuration file..

sudo nano /etc/apache2/sites-available/000-default.conf

Then add the highlighted portion to the bottom of the file.

Redirect to same domain on HTTPS only

The configuration above redirect all requests to use HTTPS only to communicate with the server with domain name example.com. So, http://example.com and http://www.example.com will be redirect to https://example.com

Https Apache Server

Step 3: Redirect Old Domain to New

If you want to redirect from an old domain to a new one, the setting below should help you do that… For this setup, HTTPS isn’t required… Run the commands below to open Apache2 default site configuration file.

sudo nano /etc/apache2/sites-available/000-default.conf

Then add the highlighted portion to the bottom of the file.

Redirect old domain to new domain

The above code added to your Apache2 site configuration file will redirect an old domain to a new one without HTTPS…. the highlighted code above redirects http://example.net and http://www.example.net to http://example.com

After making the changes above, run the commands below to test your settings and enable Apache2 rewrite and SSL modules.

If you don’t see any error messages then you’re good. Restart Apache2 web server by running the commands below.

sudo systemctl restart apache2.service

That’s it! This is how to configure Apache2 HTTP 301 redirects

~Enjoy~

You may also like the post below: