简体   繁体   中英

Apache Virtual Host SSL with different domain namess

I have a web app running on a VPS and two domain names let's say mywebsite.fr and mywebsite.com

There are all (www.mywebsite.fr // mywebsite.fr // www.mywebsite.com // mywebsite.com) pointing to the IP adress from the server where Apache is running (A record).

SSL certificate was generated for these domains.

I am a beginner with Apache. My objective is to force redirection to https://mywebsite.com/requested_route no matter what is the initial point of entry.

I did not touch .htaccess. I tried several apache conf and this one is close to get the expected outcome :

<VirtualHost *:80>
        ServerName  mywebsite.com
        DocumentRoot /var/www/mywebsite/public
        <IfModule mod_rewrite.c>
                Options -MultiViews
                RewriteEngine On
                RewriteCond %{HTTP_HOST} !^mywebsite\.com$ [NC]
                RewriteRule ^ https://mywebsite.com%{REQUEST_URI} [R=301,L]
        </IfModule>
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerAdmin myemail@gmail.com
        ServerName  mywebsite.com
        DocumentRoot /var/www/mywebsite/public
        <Directory /var/www/mywebsite/public>
                AllowOverride None
                Order Allow,Deny
                Allow from All

                <IfModule mod_rewrite.c>
                        Options -MultiViews
                        RewriteEngine On
                        RewriteCond %{REQUEST_FILENAME} !-f
                        RewriteRule ^(.*)$ index.php [QSA,L]
                </IfModule>
        </Directory>

        # (Optional) Disable the RewriteEngine for the bundles asset directories
        <Directory /var/www/mywebsite/public/bundles>
                <IfModule mod_rewrite.c>
                        RewriteEngine Off
                </IfModule>
        </Directory>

        ErrorLog /var/log/apache2/mywebsite_error.log
        CustomLog /var/log/apache2/mywebsite_access.log combined
        SSLCertificateFile /etc/letsencrypt/live/mywebsite.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/mywebsite.com/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

I have the following results with this conf :
- http://www.mywebsite.com -> https://mywebsbite.com -> OK
- http://www.mywebsite.fr -> https://mywebsbite.com -> OK
- http://mywebsite.com -> https://mywebsbite.com -> OK
- http://mywebsite.fr -> https://mywebsbite.com -> OK
- https://www.mywebsite.com -> https://mywebsbite.com -> OK
- https://www.mywebsite.fr -> https://mywebsite.fr -> NOT OK
- https://mywebsite.fr -> https://mywebsite.fr -> NOT OK
- https://mywebsite.com -> https://mywebsite.com -> OK (input=desired outcome)

I tried to rewrite on 443 for .fr to go to .com but I get into too many redirects error or ssl error.

What should I change to redirect an user to https://mywebsite.com/any_route if he comes from .fr with https?

Thank you for your help

If I understand, you want to redirect every request from other domains and sub-domains to https://mywebsite.com .

To do that, try this (Don't use RewriteEngine On twice in your any apache conf file unless you turn it off.)

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [OR] #the backward slash is used to escape the dot
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://mywebsite\.com/$1 [R=301,L]

To be clear, the first rewrite condition checks if the host is mywebsite.com.

The second rewrite condition checks if HTTPS is on.

If any of the conditions are true, the rewrite rule redirects the website to the domain https://mywebsite.com with the url like https://mywebsite.com/something .

The total conf would be like:

<VirtualHost *:80>
        ServerName  mywebsite.com
        DocumentRoot /var/www/mywebsite/public
        <IfModule mod_rewrite.c>
                Options -MultiViews
                RewriteEngine On
                RewriteCond %{HTTP_HOST} !^mywebsite\.com$ [OR]
                RewriteCond %{HTTPS} !=on
                RewriteRule ^(.*)$ https://mywebsite\.com/$1 [R=301,L]
        </IfModule>
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerAdmin myemail@gmail.com
        ServerName  mywebsite.com
        DocumentRoot /var/www/mywebsite/public
        <Directory /var/www/mywebsite/public>
                AllowOverride None
                Order Allow,Deny
                Allow from All

                <IfModule mod_rewrite.c>
                        Options -MultiViews
                        RewriteEngine On
                        RewriteCond %{REQUEST_FILENAME} !-f
                        RewriteRule ^(.*)$ index.php [QSA,L]
                        RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [OR]
                        RewriteCond %{HTTPS} !=on
                        RewriteRule ^(.*)$ https://mywebsite\.com/$1 [R=301,L]
                </IfModule>
        </Directory>

        # (Optional) Disable the RewriteEngine for the bundles asset directories
        <Directory /var/www/mywebsite/public/bundles>
                <IfModule mod_rewrite.c>
                        RewriteEngine Off
                </IfModule>
        </Directory>

        ErrorLog /var/log/apache2/mywebsite_error.log
        CustomLog /var/log/apache2/mywebsite_access.log combined
        SSLCertificateFile /etc/letsencrypt/live/mywebsite.com/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/mywebsite.com/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

(Maybe replace the word "mywebsite" to your site name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM