[英]Resolving http://www.example.com to permanently redirect to https://example.com via htaccess
[英]http://example.com would not redirect to https://www.example.com
我们的网络服务器上有两个conf。
http:
<VirtualHost *:80>
ServerName www.example.de
ServerAlias example.de www.example.at example.at www.example.net example.net
RewriteEngine On
RewriteCond %{HTTP_HOST} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}
# Error page is just the index telling about the situation of not being connected
ErrorDocument 404 /index.html
</VirtualHost>
https:
<IfModule mod_ssl.c>
NameVirtualHost *:443
<VirtualHost *:443>
ServerName www.example.de
ServerAlias example.de www.example.at example.at www.example.net example.net
SSLEngine On
SSLCertificateFile /etc/letsencrypt/live/www.example.de/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.de/privkey.pem
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ %{REQUEST_SCHEME}://www.%{HTTP_HOST}$1 [R=301,L]
DocumentRoot /var/www/homepage/web
<Directory /var/www/homepage/web>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/example-ssl.error.log
CustomLog ${APACHE_LOG_DIR}/example-ssl.access.log combined
</VirtualHost>
</IfModule>
当我尝试打开http://www.example.com时 ,它将重定向到https://www.example.com
当我尝试打开http://example.com或https://example.com时 ,出现错误“页面不可用”
我在Web服务器上的两个conf文件有什么问题? 两者都应重定向到https://www.example.com网址。
请尽量避免使用mod_rewrite,除非没有其他选择。 如您所见,它很复杂,会使您的生活变得更简单。
单个重定向将把所有内容重定向到SSL虚拟主机:
Redirect / https://www.example.de/
注意:重定向取决于mod_alias,因此无需在非SSL虚拟主机中使用RewriteEngine即可。
如果您坚持使用mod_rewrite是因为您有很多名称并且需要变量:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*) https://www.%{HTTP_HOST}/$1 [R,L]
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [R,L]
第一个捕获主机不是以www开头的情况,第二个捕获其余情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.