如何在 Apache 位置强制使用 SSL (https)

Joe*_*ams 7 ssl https svn

我正在尝试在 mod_dav_svn 提供的 SVN 存储库上强制使用 SSL (https)。这是我所拥有的:

<Location /svn/projectname>
  DAV svn
  SVNPath /var/repo/projectname
  Require valid-user
  AuthType Basic
  AuthName "Subversion repository"
  AuthUserFile /etc/svn-auth-projectname

  #here's what I tried (didn't work)
  SSLCipherSuite HIGH:MEDIUM
</Location>
Run Code Online (Sandbox Code Playgroud)

但是,当我通过 http 登录时,我不会被重定向到 https;它停留在 http 中。为什么以上不起作用?我如何让这个 https 重定向工作?

我已经看到有关使用 mod_rewrite 的建议,例如:

# /dir/.htaccess
RewriteEngine on
RewriteCond %{SERVER_PORT}!443
RewriteRule ^(.*)$ https://www.x.com/dir/$1 [R,L] 
Run Code Online (Sandbox Code Playgroud)

但是,我不完全明白这是做什么的,所以我害怕使用它。另外,它看起来更像是一个丑陋的黑客而不是正确的解决方案。

xen*_*tek 9

它不是一个黑客。这是为您提供的快速细分:

# Turn on Rewriting
RewriteEngine on 

# Apply this rule If request does not arrive on port 443
RewriteCond %{SERVER_PORT} !443 

# RegEx to capture request, URL to send it to (tacking on the captured text, stored in $1), Redirect it, and Oh, I'm the last rule.
RewriteRule ^(.*)$ https://www.x.com/dir/$1 [R,L]
Run Code Online (Sandbox Code Playgroud)


Mic*_*ael 8

我们使用略有不同但大致相同的语法。我们不会检查接收请求的端口,而是检查是否未使用 HTTPS。我们使用%{HTTP_HOST}环境变量而不是硬编码主机名。

  RewriteEngine              On
  RewriteCond     %{HTTPS}   Off
  RewriteRule     .*         https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Run Code Online (Sandbox Code Playgroud)

我更喜欢这种方法,因为它在 Apache 侦听非标准端口时有效。%{HTTP_HOST}如果您的站点位于代理之后,则使用可能会出现问题,但我们还没有尝试过。