简体   繁体   中英

Why is apache redirecting to one of ServerAlias?

I am using two top level domains .pl and .net . Both should always redirect to https version however when user go to http://example.net/ apache redirects to https://example.pl/ . Why it is preferable even when it is on third position in ServerAlias ? Apache redirect to .net only if I remove .pl from ServerAlias .
My /etc/apache2/sites-enabled/example.pl.conf looks like this:

<VirtualHost *:80>
   ServerName example
   DocumentRoot /var/www/example.pl
   ServerAlias example.net www.example.net example.pl www.example.pl
</VirtualHost>

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www/example.pl

        <Directory "/var/www/example.pl">
                Options FollowSymLinks
                AllowOverride All
                Require all granted
        </Directory>

        ServerName example

        LogLevel trace8 rewrite:trace8
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/2_example.pl.crt
        SSLCertificateKeyFile /etc/ssl/private/example.key
        SSLCertificateChainFile /etc/ssl/certs/1_root_bundle.crt

        <FilesMatch "\.(cgi|shtml|phtml|php)$">
                SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
                SSLOptions +StdEnvVars
        </Directory>

        BrowserMatch "MSIE [2-6]" \
                nokeepalive ssl-unclean-shutdown \
                downgrade-1.0 force-response-1.0
        BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

    </VirtualHost>
</IfModule>

And my .htaccess :

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^www\.(.*)
    RewriteRule ^.*$ https://%1/$1 [R=301,L]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Why making it so complicated with .htaccess if you have access to the main conf?

Defining the virtulahosts should be enough like:

#NameVirtualHost *:80 <-- uncomment only with 2.2
<VirtualHost *:80>
ServerName example.net
Redirect / https://example.net/
</VirtualHost>

<VirtualHost *:80>
ServerName example.pl
Redirect / https://example.pl/
</VirtualHost>

Or if you are lazy and just want to define one virtualhost:

<VirtualHost *:80>
ServerName bogus.example
RewriteEngine on
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [R,L]
</VirtualHost>

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