首页 > 解决方案 > 对于使用 modperl 的站点,使用 Apache RedirectPermanent 将 HTTP 重定向到 HTTPS

问题描述

modperl我有一个 Apache 2.4 站点,它提供通过 HTTPS生成的内容。相关Location部分如下:

<Location />
  SetHandler modperl
  PerlResponseHandler MyService
</Location>

我现在也想通过 HTTP 启用相同的内容。下面的/etc/apache2/sites-enabled/myservice.conf工作在某种程度上是因为它重定向带有尾随路径的 URL,而不是“根”页面(例如,http://myservice.mycompany.com)。我怀疑这是(也)因为此页面的特殊之处在于它由modperl.

<VirtualHost *:80>
  LogLevel debug
  ServerName myservice.mycompany.com
  RedirectPermanent / https://myservice.mycompany.com
</VirtualHost>

那么,如何RedirectPermanent将整个 Apache 站点从 HTTP 重定向到 HTTPS,包括那些支持的页面modperl

标签: apacheredirecthttps

解决方案


要重定向站点中的任何路径,您可能需要使用mod_rewrite. 这些规则应该有效:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]

(我已经从这个问题中删除了perlandmod_perl标记。重写发生在服务器关心它将如何提供内容之前很久。)


推荐阅读