logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: Help redirecting all subdomains to https
Author
NotionCommotion



Joined: 12 Nov 2014
Posts: 7

PostPosted: Wed 21 Jan '15 14:38    Post subject: Help redirecting all subdomains to https Reply with quote

Say I have two domains: example.com and example.net.

Upon a http request to example.com, I wish to redirect to https://example.com.

Upon either a http or https request to example.net, I wish to redirect to https://example.com.

Note that for both cases, subdomains should also be redirected, and the change should be considered permanent.

I've seem multiple ways to do so. What is the best way? How do I deal with subdomains when there is a new domain such as example.net to example.com?

I've taken a stab. Can anyone help fill in the gaps?

Thank you

Code:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com
    ServerSignature Off
    Redirect permanent / https://%{SERVER_NAME}/
    # or ???
    RewriteEngine on
    RewriteCond %{HTTPS} !=on
    RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [NE,R,L]
    # or ???
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
</VirtualHost>

<VirtualHost *:80>
    ServerName example.net
    ServerAlias *.example.net
    ServerSignature Off
    RewriteEngine on
    RewriteCond %{HTTPS} !=on
    # How do I deal with subdomains?
    RewriteRule .* https://example.com%{REQUEST_URI} [NE,R,L]
</VirtualHost>



Also, within the 443 VirtualHost, I've see redirects if not ssl. Is this desired? If so, why?

Code:
<VirtualHost *:443>
    ServerName example.com
    ServerAlias *.example.com
    DocumentRoot /var/www/html
    SSLEngine on
    <Directory "/var/www/html">
        <IfModule mod_rewrite.c>
            RewriteEngine On
            RewriteBase /
            # Are these lines necessary, or should I create a virtual host for http on port 80 instead?
            RewriteCond %{HTTPS} !=on
            RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [NE,R,L]
        </IfModule>
    </Directory>
</VirtualHost>


Lastly, how would I redirect a request with no subdomain (i.e. https://example.com) to https://www.example.com?
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7373
Location: Germany, Next to Hamburg

PostPosted: Sun 25 Jan '15 1:09    Post subject: Reply with quote

The rule inside the ssl vhost is crap Wink

You need only the rule inside the port 80 vhost. And yes you need one http port 80 vhost and one https port 443 vhost

I use
Code:

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(products) https://%{SERVER_NAME}%{REQUEST_URI} [R,L]


redirect a new domain:
in the old vhost or the old server
Code:

RewriteEngine On
RewriteRule (.*) http://newdomain.com%{REQUEST_URI}
Back to top


Reply to topic   Topic: Help redirecting all subdomains to https View previous topic :: View next topic
Post new topic   Forum Index -> Apache