为什么apache重定向到ServerAlias之一?

时间:2016-10-07 17:30:17

标签: apache .htaccess redirect

我使用的是两个顶级域.pl.net。两者都应该始终重定向到https版本,但是当用户转到http://example.net/ apache重定向到https://example.pl/时。即使在ServerAlias的第三个位置,为什么它更可取?仅当我从.net删除.pl时,Apache才会重定向到ServerAlias 我的/etc/apache2/sites-enabled/example.pl.conf看起来像这样:

<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>

我的.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>

1 个答案:

答案 0 :(得分:1)

如果你有权访问主conf,为什么要使用.htaccess这么复杂?

定义virtulahosts就足够了:

#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>

或者如果你很懒,只想定义一个虚拟主机:

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