Apache Rewriterule Http To Https



HTTP connections can be redirected to HTTPS using the apache mod_rewrite module. Mod_rewrite should be available in every apache installation.

  1. Basically instead of having to check for HTTPS using a RewriteCond for every redirect that can be HTTP or HTTPS, I found out I can set an environment variable 1 time to determine if HTTP or HTTPS is being used for that request, and by giving the variable a value of 'https' for HTTPS or 'http' for HTTP I can remove all duplicate rewriterule blocks.
  2. Apache Redirect HTTP to HTTPS using modrewrite Apache’s modrewrite makes it easy to require SSL to be used on your site and to gently redirect users who forget to add the https when typing the URL. Using Apache to redirect http to https will make sure that your site (or a part of it) will only be accessed by your customers using SSL.

Force Redirect HTTP to HTTPS in Apache – Many of sites required to always running with SSL only. And we need to ensure that every user must access the website through SSL. If any user tried to access the website with non-ssl URL, He must be a redirect to SSL website.

Apache HTTP to HTTPS Redirect

Create a file with the name .htaccess in the website root directory which contains the following lines:

If you use ISPConfig 3, you can add these lines also in the apache directives field of the website settings form instead of adding them to a .htaccess file. Or use the HTTP to HTTPS redirect option on the redirect tab of the site. But the approach with the .htaccess file will work on ISPConfig as well.

In the previous article, we described on How to Setup Cloudflare SSL and Configure Origin Certificate for Apache. Now, if you have SSL certificate active on your domain, then you may want to force all requests to https. In this guide, you will learn how to force redirect incoming requests from http to https in Apache and Nginx web server.

Force Redirect in Apache Server

Using virtual host configuration file

Apache

In your Apache virtual host configuration file, you can add these at the end to force redirect http and www to non-www https.

So, the virtual host configuration file will look like this:

Here, you will also need to have a virtual host file for port 443.

After, you will need to reload Apache virtual host configuration file using the following command:

Or, you can restart Apache web server using any of the following commands:

Apache

Using .htaccess file in Apache

In your Apache .htaccess for your document root, you can add these to force redirect http and www to non-www https. Here, you need to replace “example.com” with your domain name.

Apache Rewriterule Syntax

So, the .htaccess file will look something like this:

Force Redirect in Nginx Server

Apache Redirection Http Vers Https

Using virtual host configuration file

In your Nginx virtual host configuration file, you can add these server blocks to force redirect http and www to non-www https.

Apache Rewriterule Redirect Http To Https

To force redirect http and non-www to https www, you can modify the server blocks like this:

After, you will need to reload Nginx virtual host configuration or restart your Nginx web server.

You can reload Nginx virtual host configuration file using the command:

Redirection

You can restart Nginx server using any of the following commands: