【发布时间】:2020-11-29 23:56:15
【问题描述】:
我已经坐了很长时间了,无法理解它。
基本上,我最终要寻找的功能类似于以下内容:
http://example.com 重定向到 https://example.com - 当前工作
为了
http://*.example.com 和 https://*.example.com 重定向到 https://example.com/* - 不起作用。目前,所有子域都被重定向到主域,没有它们各自的文件夹路径。此外,当转到https://test.example.com 时,它根本不会重定向。
这是来自sites-enabled 的example.com.conf:
<IfModule mod_ssl.c>
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
# Redirect permanent / https://example.com/
<Directory /var/www/example.com/public_html>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<IfModule mod_rewrite.c>
# Redirect all traffic to HTTPS
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,R=301,L]
</IfModule>
</VirtualHost>
<VirtualHost _default_:443>
ServerAdmin webmaster@localhost
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<IfModule mod_rewrite.c>
# Remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [QSA,R=301,L]
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/%1/$1 [R=301,L]
</IfModule>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/ssl/civrpssl/example.com.crt
SSLCertificateKeyFile /etc/ssl/civrpssl/example.com.key
SSLCACertificateFile /etc/ssl/civrpssl/ca-example.com.crt
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
</IfModule>
最后,这是来自example.com/public_html/ 的.htaccess:
removed, see edit #2.
我希望这是一个我没有看到的相当明显的问题。否则,请有人告诉我我应该从哪里开始寻找问题?我假设 HTTPS 重定向会干扰 .htaccess 文件中的子域重定向,但我不知道我该如何改变它。
编辑 #1: 我刚刚注意到 www.example.com 抛出错误“找不到 URL”,而不是重定向到 https://example.com。 .htaccess第一次重写怎么没抓到?
编辑 #2:
- 将重写从
.htaccess移至example.com.conf(代码已在帖子中更新)。 - 为端口 80 添加了 VHost,本质上只是将所有流量重定向到 HTTPS。我发现重定向有效,但是它不会在重定向中包含子域,所以
http://test.example.com只会导致https://example.com。 - 跑
a2enmod rewrite,因为显然它没有启用。
【问题讨论】:
-
80 端口的 vHost 怎么样?您发布的虚拟主机仅对
example.com的请求除外 - 不接受任何子域(包括www)。但是,如果这是您的配置中定义的第一个 vHost,那么它可能仍会捕获这些请求。否则,您的.htaccess指令看起来“正常”,除非您访问服务器配置,那么理想情况下,您应该在服务器配置中重定向 HTTP 和子域。 -
感谢您的反馈!我已经更新了帖子。如果你有时间,请再看看。
标签: .htaccess mod-rewrite apache2