我使用了两个顶级域名.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>
发布于 2016-10-08 01:43:15
如果你可以访问主会议,为什么要用.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>
https://stackoverflow.com/questions/39922918
复制相似问题