Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a development site up that requires a username and password (basic http auth) before the user can see the site. I'd like to redirect to a secure protocol first, before the user can send in the password in clear text. Any thoughts on how to do this using Apache? I have access to the conf and .htaccess files.

The end results would be:

http://xxxx/ -- When user goes here, they get immediately redirected to https:

https -- When user gets here, they get prompted for username / password.

share|improve this question

5 Answers 5

up vote 3 down vote accepted

You can use the redirect directive on http virtualhost to redirect to the https site where authentification is done. You could also use mod_rewrite to do the redirection. The basic things is just to not set authentification on http virtualhost and redirect everything on the https virtualhost where authentication is done.

share|improve this answer
The issue is these are the same host, domains are the same. Is there a way in the .htaccess to say only apply the auth if https is set? – Jay Oct 16 '09 at 21:36
Sadly this can only be done in a virtualhost, as apache doesn't provide a way to apply different configuration per request at the .htaccess level. You can use the different virtualhosts on different ports to do things differently. – David Pashley Oct 16 '09 at 22:36
Yes, you need to use 2 virtual host like <VirtualHost *:80> and <VirtualHost *:443> and not only one – radius Oct 16 '09 at 23:04
While I was hoping to be able to do this in .htaccess, looks like you guys are right, the only way is in the conf files, so I set it up to redirect http to https. Auth is still taken care of in the .htaccess, which is run only when accessing the https site now. Keeping auth in .htaccess has the benefit that if redirection fails for any reason, auth is still used. – Jay Oct 17 '09 at 17:37

Regardless of how you have the SSL vhost configured, I'd use this configuration for the non-SSL vhost:

<VirtualHost *:80>

    RedirectMatch ^/(.*)$1 [L,R]


Add lines for your logging, too, but nothing else is needed. Everything will be redirected permantently to the https:// URL, and the SSL site's .htaccess or other access control stuff won't be handled until after the redirect.

share|improve this answer

You can also see that blog entry here which explains how to do this with SSLRequireSSL, a custom ErrorDocument 403 directive which points to a perl script which redirects to the correct HTTPS url.

share|improve this answer

Our client's webapp is installed in his webuser directory. Authorisation is handled before mod_rewrite rules (, and we could not get the accepted answer to work, so mod_rewrite seemed not an option.

Eventually we explicitly required SSL and used the webapp's root over HTTPS as 403 and 404 error documents. So when one visits any page over HTTP (which is unauthorized, hence the 403) or a non existing page (404), he is being redirected to ie. https://DOMAIN.TLD/~WEBUSER/admin.

This is the .htaccess file with some extra info in the comments.

### INFO: Rewrites and redirects are handled after authorisation
### @link

### INFO: Log out of a HTPASSWD session
### This was not always possible, but Firefox and Chrome seem to end sessions
### when a new one is trying to be using ie.:
### https://logout:logout@DOMAIN.TLD/~WEBUSER/
### @link

### FORCE SSL: Explicitly require the SSL certificate of a certain domain to
### disallow ie. unsigned certificates. ErrorDocument's are used to redirect
### the user to an HTTPS URL.
### @link
SSLOptions +StrictRequire

AuthUserFile /var/www/vhosts/DOMAIN.TLD/web_users/WEBUSER/.htpasswd
AuthType Basic
AuthName "Hello"
Require valid-user

### ERROR DOCUMENTS: Redirect user in case of a 403 / 404.
ErrorDocument 403 https://DOMAIN.TLD/~WEBUSER/admin
ErrorDocument 404 https://DOMAIN.TLD/~WEBUSER/admin
share|improve this answer

If I recall correctly, http basic authentication is done at the initial handshake before the SSL session is established. So even if you are logging into an SSL server the basic authentication is done in plaintext. That has been one of the longstanding issues with basic authentication. Perhaps one of the other http authentication methods deals with this. It's been a long time since I've looked at this.

share|improve this answer
This is not true. HTTP Auth data is transmitted in the headers of the request, which is done after SSL negotiation. "Basic authentication across an SSL connection, however, will be secure, since everything is going to be encrypted, including the username and password." - – Jay Mar 7 '11 at 14:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.