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

我试图在由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> 

但是,当我通过httplogin时,我不会redirect到https; 它保持在http。 为什么没有上述工作? 我如何获得这个httpsredirect工作?

我见过有关使用mod_rewrite的build议,例如:

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

但是,我不明白这是什么,所以我不敢使用它。 此外,它看起来更像是一个丑陋的黑客比正确的解决scheme。

它不是黑客。 这里是你的快速分解:

 # 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] 

我们使用稍微不同的语法,但大多是等同的语法。 而不是检查请求收到的端口,我们检查HTTPS没有被使用。 我们使用%{HTTP_HOST}环境variables而不是硬编码主机名。

  RewriteEngine On RewriteCond %{HTTPS} Off RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L] 

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

我们用名字虚拟主机来定义一切。 那么,如果你在一个<Virtualhost *:80>定义中,你不必检查它是不是端口443,你已经知道它不是。 然后你可以强制所有命中80到443的规则如下:

 RewriteEngine On RewriteRule ^(.)$ https://www.yourdomain.com/$1 [R,L] 

除了已经提到的redirect之外 ,如果不使用HTTPS连接,则可能需要将SSLRequireSSL指令添加到位置容器,否则它将拒绝访问。 但是,只有在*:443上监听的SVN站点的VirtualHost解决scheme更加优雅。

尝试

  <Location /> SSLRequireSSL </Location> 

假设Apache 2:

除了事实上启用SSL外,您已经完成了所有工作。 使用“SSLEngine”,“SSLCertificateFile”和“SSLCertificateKeyFile”命令,如下所述:

您需要为SSLCertificate * File命令生成和/或购买PKI文件(SSL证书和相关私钥文件)。

http://www.debianhelp.co.uk/apacheinstall.htm

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

这说:

  1. 启用URLredirect和重写支持
  2. 对于所有不通过端口443的连接(换句话说,所有非SSL连接)
  3. 将它们redirect到https://www.x.com/dir/中的相同页面&#x3002; 因此,如果他们要求http://www.x.com/y ,他们将在SSL服务器的目录下redirect到x: https : //www.x.com/dir/x

如果您的站点现在支持SSL,而您只是试图在svn子目录上强制使用SSL,则可以尝试如下所示:

 1. RewriteEngine on 2. RewriteCond %{SERVER_PORT}!443 3. RewriteRule ^/svn/(.*)$ https://www.x.com/svn/$1 [R,L] 

哪一个会像上面一样,但只能用在subversion目录下的东西。

你想要做的是强制redirecthttps的常规networking连接。 重写规则似乎完成了。 这不是一个丑陋的黑客,而是正确的做法。 您目前的configuration不强制redirect

在位置块中使用SSLRequireSSL