I'm trying to force SSL (https) on an SVN repository served by mod_dav_svn. Here's what I have:

<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)

However, I don't get redirected to https when I log in via http; it stays in http. Why doesn't the above work? How do I get this https redirect to work?

I've seen suggestions about using mod_rewrite, e.g.:

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

However, I don't understand exactly what this does, so I'm afraid to use it. Plus, it looks more like an ugly hack than the correct solution.

  • You can not do the redirect and th authentication in the same virtualhost. Any attempt to do so will cause the authentication to happen in HTTP, before the redirection to HTTPS! That is NOT good! You have to do them in separate virtualhosts. Redirect in a HTTP vhost and authentication in a HTTPS vhost.
    – anthony
    Commented Jan 24, 2018 at 6:40

8 Answers 8


Its not a hack. here's a quick breakdown for you:

# 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]
  • What would cause bad argument line '%{SERVER_PORT}!443' when using this?
    – philfreo
    Commented Jan 21, 2010 at 1:25
  • not sure. do you have SSL already configured for this vhost?
    – xentek
    Commented Mar 22, 2010 at 23:27
  • 1
    The example is not quite correct. You need a space before the !443.
    – Zed
    Commented Jun 15, 2010 at 20:01
  • & @Zed, fixed it. Does require the space in there.
    – Chris S
    Commented Feb 24, 2011 at 19:22

We use a slightly different, but mostly equivalent syntax. Rather than checking the port the request was received on, we check that HTTPS isn't being used. And we use the %{HTTP_HOST} environment variable rather than hardcoding the host name.

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

I like this approach a little better, because it works when Apache is listening on non-standard ports. There could be a problem with using %{HTTP_HOST} if your site is behind a proxy, but we haven't tried that yet.

  • 1
    I think the %{HTTPS} thing is only available in Apache 2.x or something like that, hence the relative prevalence of checking the port in examples found on the net. This is a better way, as you pointed out.
    – Chris S
    Commented Feb 24, 2011 at 19:24


 <Location />

We define everything with name Virtual hosts. Then, if you are within a <Virtualhost *:80> definition, you don't have to check if it is not port 443, you already know it's not. You Can then just force everything that hits 80 over to 443 with a rule like:

RewriteEngine On
RewriteRule ^(.)$ https://www.yourdomain.com/$1 [R,L]
  • Totally agree on the vhosts approach. I find this works and saves firing up the rewrite engine: RedirectMatch 301 (.*) https://%{HTTP_HOST}$1
    – Synchro
    Commented May 5, 2011 at 15:13
  • RewriteRule ^/(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R]
    – drafael
    Commented Nov 21, 2011 at 14:09

Additionally to the already mentioned redirect you might want to add the SSLRequireSSL directive to your Location container which will deny access if you do not use an HTTPS connection. However, the solution with a VirtualHost for your SVN site which only listens on *:443 is more elegant.

  • If we are talking about security, whe want to use a protocol, not use a specific port : this is secured by the firewall... Commented Feb 28, 2020 at 10:29

Assuming Apache 2:

You've done everything except actually enable SSL by the looks of things. Use the "SSLEngine", "SSLCertificateFile", and "SSLCertificateKeyFile" commands, as described here:

You'll need to generate and/or buy an the PKI files (SSL certificate and related private key file) for the SSLCertificate*File commands.


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

This says:

  1. Enable URL redirection and rewriting support
  2. For all connections that don't come through port 443 (in other words, all non-SSL connections)
  3. Redirect them to the same page under https://www.x.com/dir/. So if they ask for http://www.x.com/y, they'll be redirected to x under the SSL server's dir: https://www.x.com/dir/x

If your site supports SSL now, and you're just trying to force SSL on the svn subdirectory, you might try something like:

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

Which would do the same as above, but only for stuff under the subversion directory.


What you want to do is to force a redirect to https for regular web connections. The rewrite rules seem to accomplish that. That is not an ugly hack but rather the correct way to do it. Your present configuration does not force a redirect.


Use SSLRequireSSL in Location block

  • 1
    Can you elaborate on this answer?
    – Dave M
    Commented Sep 23, 2013 at 12:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .