首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么apache要重定向到ServerAlias之一?

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

Stack Overflow用户
提问于 2016-10-08 01:30:17
回答 1查看 729关注 0票数 0

我使用了两个顶级域名.pl.net。两者都应该始终重定向到https版本,但是当用户转到http://example.net/时,apache会重定向到https://example.pl/。为什么即使它在ServerAlias中排在第三位也是更可取的?只有当我从.net中删除.pl时,Apache才会重定向到ServerAlias

我的/etc/apache2/sites-enabled/example.pl.conf看起来像这样:

代码语言:javascript
运行
复制
<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

代码语言:javascript
运行
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2016-10-08 01:43:15

如果你可以访问主会议,为什么要用.htaccess把事情搞得这么复杂呢?

定义virtulahosts应该足够类似于:

代码语言:javascript
运行
复制
#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>

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

代码语言:javascript
运行
复制
<VirtualHost *:80>
ServerName bogus.example
RewriteEngine on
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [R,L]
</VirtualHost>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39922918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档