Redirecting https requests to a subdomain with Apache2 and mod_rewrite

Posted on

Redirecting https requests to a subdomain with Apache2 and mod_rewrite – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about linux, apache-2.2, ssl, mod-rewrite, .

I have a site that has an SSL certificate on a subdomain, let’s call it https:// ssl.maindomain.com. I want to redirect all https requests on https:// maindomain.com to https:// ssl.maindomain.com, while still keeping all http requests on http:// maindomain.com.

Is this even possible? I’ve googled and nothing really came up.

(The spaces in the protocols are because I cannot post hyperlinks)

Thanks,
Luke

edit: Figured it out. This did the trick.

RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ https://ssl.maindomain.com%{REQUEST_URI} [R,L]

Solution :

Use the rewrite module in apache … the lines you need to place into the vhost for maindomain.com would look something like this (please test first, this is untested):

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) https://ssl.maindomain.com$1

Basically, turn on the engine (line 1), if the the https is “on” (line 2) execute the next line: rewrite the url to ssl main domain (line 3). The third line would work like this:

https://maindomain.com/mypage to https://ssl.maindomain.com/mypage

https://maindomain.com/ to https://ssl.maindomain.com/

This would only trigger on the condition (RewriteCond) that https is on. When https is off, i.e., http, your traffic should continue go to http://maindomain.com/

Try it out in a test server before running this in production in case this interferes with some other rules. HTH.

Leave a Reply

Your email address will not be published. Required fields are marked *