00 Votes

Redirect all HTTP-Requests to HTTPS

Question by Guest | 2017-08-04 at 17:16

I have purchased a SSL certificate for my website. Before, when my page had no SSL encryption, my webpage was accessible via HTTP, for example via a URL like http://www.example.com/page, now it is additional reachable via the HTTPS-URL equivalent https://www.example.com/page.

Now, there is the problem that all links directing to my site are still the old HTTP links and of course, also each search engine has still stored my old links (double content - bad).

So, is there any possibility to redirect each request coming over HTTP to HTTPS? This would solve this problem. However, ideally, the rest of the link should be kept and the redirection should be exactly, I mean that http://www.example.com/page should be redirected to https://www.example.com/page and not only to https://www.example.com.

ReplyPositiveNegativeDateVotes
1Best Answer1 Vote

You can easily create such an HTTP to HTTPS redirection by using HTACCESS.

Just insert the following lines directly to the beginning of your .htaccess file (a file named .htaccess located in the root directory of your web project):

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

The first line activates the Rewrite-Engine. In the following second line (Rewrite-Condition) it is checked whether the request is not made via HTTPS (HTTPS off). If this should be the case, the third line (Rewrite-Rule) is executed and the redirection is carried out.

As you can see, we are also sending the status code 301 (Permanent Redirect). This should show all search engines and browsers that this rerouting is permanent and not only temporary. So, browser and search engines have the opportunity to update their URLs.
2017-08-04 at 17:26

ReplyPositive Negative
00 Votes

Of course the answer is correct. Alternatively, you can also construct this redirection in some other ways.

For example, instead of "RewriteCond %{HTTPS} off" you can also write "RewriteCond %{HTTPS} != on" or "RewriteCond %{SERVER_PORT} !^443$". With HTTPS, the port 443 is used. Therefore, you can also ask for this port for checking whether the request is running over HTTPS/SSL.

You can also change the RewriteRule, for example to "RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]" or we just hard code our domain directly: RewriteRule (.*) https://www.example.com/$1 [R=301,L];
2017-08-04 at 17:43

ReplyPositive Negative
Reply

Related Topics

HTACCESS: Simplify URL

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.