重定向* .example.com&的问题从HTTPS的example.com到www.example.com

编程入门 行业动态 更新时间:2024-10-23 18:25:49
本文介绍了重定向* .example&的问题从HTTPS的example到www.example的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们有一个网站,我将其称为example.大多数情况下,您会看到 www.example ,有时我们会将您重定向到 www.example .

We have a site I'll call example. Most of the time you see www.example and sometimes we redirect you to www.example.

我们希望将任何人重定向到 example 或*.example. com到 www.example ,对于https也是一样. (主要是避免访问 example 而不是 www.example )

We want to redirect anyone going to example or *.example to www.example, and the same for https. (It's mainly to avoid the alert you get if you go to example instead of www.example)

我们的虚拟主机文件位于帖子的末尾. 除了可以很好地完成一种奇怪的行为:

Our vhost file is at the end of the post. It works nicely except for one strange behavior:

  • example ->成功重定向到 www.example
  • www.example ->无法成功重定向
  • foo.example ->成功重定向到 www.example
  • example ->成功重定向到 www.example
  • www.example ->无法成功定向
  • foo.example -> 错误-重定向到 www.example
  • example -> successfully redirects to www.example
  • www.example -> successfully does not redirect
  • foo.example -> successfully redirects to www.example
  • example -> successfully redirects to www.example
  • www.example -> successfully does not direct
  • foo.example -> ERROR - redirects to www.example

这是我无法理解的最后结果.我已经尝试了Google&的许多反复试验解决方案.堆栈溢出,但似乎没有任何改变.即使我们交换配置顺序(所以443在80之前),它仍然会重定向 foo.example. com 到 www.example

It's this last result I can't fathom. I've tried a lot of trial and error solutions from Google & Stack Overflow but nothing seems to change it. Even if we swap the order of the configurations (so that 443 is before 80) it still redirects foo.example to www.example

我们正在Ubuntu上运行Apache/2.2.12.

We are running Apache/2.2.12 on Ubuntu.

这是配置文件:

<VirtualHost *:80> ServerName www.example ServerAlias example *.example ServerSignature On DocumentRoot /var/www/example/public RailsEnv 'production' PassengerHighPerformance on <Directory /var/www/example/public> AllowOverride all Options -MultiViews </Directory> SSLEngine Off CustomLog /var/log/apache2/example.log combined ErrorLog /var/log/apache2/example-error.log # Possible values include: debug, info, notice, warn, error, crit, alert, emerg. LogLevel warn RewriteEngine On RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^[^\./]+\.[^\./]+$ RewriteRule ^/(.*)$ www.%{HTTP_HOST}/$1 [R=301,L] </VirtualHost> <VirtualHost *:443> ServerName www.example ServerAlias example *.acome DocumentRoot /var/www/example/public RailsEnv 'production' PassengerHighPerformance on <Directory /var/www/example/public> AllowOverride all Options -MultiViews </Directory> SSLCertificateFile /etc/ssl/certs/www.example.crt SSLCertificateKeyFile /etc/ssl/private/example.private.key SSLCACertificateFile /etc/ssl/certs/EV_intermediate.crt SSLEngine On CustomLog /var/log/apache2/ssl-example.log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b" ErrorLog /var/log/apache2/ssl-example-error.log # Possible values include: debug, info, notice, warn, error, crit, alert, emerg. LogLevel warn RewriteEngine On RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} ^[^\./]+\.[^\./]+$ RewriteRule ^/(.*)$ www.%{HTTP_HOST}/$1 [R=301,L] </VirtualHost>

推荐答案

感谢所有花时间阅读问题或(甚至更好)尝试和帮助的人.这就是使Stack Overflow如此有用的资源的原因.

Thanks to everyone who took the time to read the question, or (even better) to try and help. It's what makes Stack Overflow such a useful resource.

我们重新阅读了TFM,了解了有关Apache重写的更多信息,这就是答案.

We re-read TFM, learnt a bit more about Apache rewrite and here's the answer.

问题1:重写规则(取自Google)旨在仅将acme重定向到www.acme,而不重定向任何其他子域.

Problem #1: The rewrite rules (taken from Google) were designed to only redirect acme to www.acme, not any other sub-domain.

问题2:出于某种原因,Chrome重定向到 www. acme 本身.当我们完全删除重写规则时,描述的行为仍然发生.

Problem #2: For some reason, Chrome was redirecting to www.acme by itself. When we removed the rewrite rules completely, the described behavior still happened.

解决方案:更改重写规则,以实际捕获除www之外的任何子域.

Solution: Change the rewrite rules to actually catch any sub-domain other than www.

请参阅下面的可行解决方案.

See below for a working solution.

还有一个问题. 如果您转到 acme (或任何子域),某些浏览器会在重定向您之前引发警告,指出SSL认证不匹配.解决此问题的唯一方法是获取通配符证书.由于我们使用的扩展验证证书本来已经非常昂贵,因此现在我们只需要忍受该警告即可.很想知道在重定向之前避免显示无效证书警告的任何变通方法.

There remains one issue. if you go to acme (or any sub-domain), some browsers will throw a warning saying the SSL certification does not match before redirecting you. The only way to fix this is to get a wildcard cert. As we're using an Extended Validation cert that was already very expensive we're just going to have to live with that warning for now. Would love to hear about any workarounds that would avoid showing an invalid cert warning before redirection.

<VirtualHost *:80> ServerName www.acme ServerAlias acme *.acme ServerSignature On DocumentRoot /var/www/acme/public RailsEnv 'production' PassengerHighPerformance on <Directory /var/www/acme/public> AllowOverride all Options -MultiViews </Directory> SSLEngine Off CustomLog /var/log/apache2/acme.log combined ErrorLog /var/log/apache2/acme-error.log # Possible values include: debug, info, notice, warn, error, crit, alert, emerg. LogLevel warn RewriteEngine On RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} ^/(stats/|missing\.html|failed_auth\.html|error/).* [NC] RewriteRule .* - [L] RewriteCond %{ENV:REDIRECT_STATUS} 200 RewriteRule .* - [L] RewriteCond %{HTTP_HOST} !^www\.[a-z-]+\.[a-z]{2,6} [NC] RewriteCond %{HTTP_HOST} ([a-z-]+\.[a-z]{2,6})$ [NC] RewriteRule ^/(.*)$ www.%1/$1 [R=301,L] </VirtualHost> <VirtualHost *:443> ServerName www.acme ServerAlias acme *.acme DocumentRoot /var/www/acme/public RailsEnv 'production' PassengerHighPerformance on <Directory /var/www/acme/public> AllowOverride all Options -MultiViews </Directory> SSLCertificateFile /etc/ssl/certs/www.acme.crt SSLCertificateKeyFile /etc/ssl/private/acme.private.key SSLCACertificateFile /etc/ssl/certs/EV_intermediate.crt SSLEngine On CustomLog /var/log/apache2/ssl-acme.log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b" ErrorLog /var/log/apache2/ssl-acme-error.log # Possible values include: debug, info, notice, warn, error, crit, alert, emerg. LogLevel warn RewriteEngine On RewriteCond %{HTTPS} on RewriteCond %{REQUEST_URI} ^/(stats/|missing\.html|failed_auth\.html|error/).* [NC] RewriteRule .* - [L] RewriteCond %{ENV:REDIRECT_STATUS} 200 RewriteRule .* - [L] RewriteCond %{HTTP_HOST} !^www\.[a-z-]+\.[a-z]{2,6} [NC] RewriteCond %{HTTP_HOST} ([a-z-]+\.[a-z]{2,6})$ [NC] RewriteRule ^/(.*)$ www.%1/$1 [R=301,L] </VirtualHost>

更多推荐

重定向* .example.com&amp;的问题从HTTPS的example.com到www.example.com

本文发布于:2023-11-01 11:39:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549244.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   amp   www   HTTPS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!