I have the following conf files:



    ServerName example.com

    RedirectPermanent / https://example.com/

#   RewriteEngine On
#   RewriteCond %{SERVER_PORT} !^443$
#   RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [L,R]

#   SSLRequireSSL
#   Redirect permanent /secure https://example.com/

#   Redirect / https://example.com/

As you can see from the commented out lines, I have tried several approaches.



    DocumentRoot "/opt/www/example-docroot"
    ServerName example.com
    DirectoryIndex index.html
    SSLEngine on

    SSLCertificateFile /etc/httpd/conf.d/ssl/example.com/csr.example.2011.pem.blade
    SSLCertificateKeyFile /etc/httpd/conf.d/ssl/example.com/nokey.example.2011.pem
    SSLCACertificateFile /etc/httpd/conf.d/ssl/example.com/CA.blade.2011.csr

    <Files ~ "\.(cgi|shtml|phtml|php3?)$">
        SSLOptions +StdEnvVars
    <Directory "/etc/httpd/cgi-bin">
        SSLOptions +StdEnvVars

Some thing removed for simplicity, names changed to protect the innocent.

I update these files on the server /etc/httpd/vhosts

then run

apachectl restart

Which does give me these warnings:

[warn] NameVirtualHost has no VirtualHosts
[warn] NameVirtualHost has no VirtualHosts

We have numerous vhosts running from this server. The above configs seem to be aligned, so I don't think these warnings are applying here. Maybe wrong.

Updating the default 80 (http) page in my browser, always shows the default http page.

Any suggestions, on how to get the redirect to work?

  • Just to confirm, I hope that's not the actual IP address in use. Commented Oct 2, 2012 at 12:53
  • Yes, that's just an example IP. Commented Oct 3, 2012 at 3:21
  • This issue has become a low priority, at the moment, or perhaps even resolved already. I'll try to look into it a bit more and whose ever answer is the closet will get the mark. Commented Oct 3, 2012 at 3:24

7 Answers 7


Many examples work on specific configurations. This one always works, no matter which configuration your Apache server uses:

RewriteEngine On
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/$1 [R=301,L]
  • What file do you you add this to?
    – Richard
    Commented Jun 26, 2017 at 14:28
  • 2
    @Richard Just got this working on CentOS, I created a new redirect.conf file under httpd/conf.d and added it within a <VirtualHost *:80> directive. I am using this with django and had to remove the trailing slash before the $1 in the redirect, to avoid an extra slash.
    – trpt4him
    Commented Jun 27, 2017 at 15:04
  • I tried many other variations on this and this is the only one that has worked for me if using non-standard ports and variables, and not hard coding the host. Add the condition using the custom port ( !10443, in my case), and the rewritten host part: https://${HTTP_HOST}:10443/$1 Commented Oct 17, 2017 at 23:54
  • When you use a simple static html website for testing, know that your browser likes to cache requests, such that your configuration is not used when testing. You may need to switch browsers or clear the cache. Commented May 3 at 21:12

I had faced exactly the same issue few days back. I tried the following in my VirtualHost config (applicable for http port 80) in apache httpd.conf file that worked.

<Virtualhost *:80>
ServerAdmin [email protected]
ServerName site.com
ServerAlias site.com www.site.com

RedirectMatch permanent ^(.*)$ https://www.site.com$1

This works like charm and you don't need any config anywhere else or any extra module.

  • Thanks. But this isn't working for me. I'm suspecting it's not so much about the config commands, but something else... Commented Aug 24, 2012 at 3:25
  • 2
    +1 for not using mod_rewrite.
    – pjmorse
    Commented Oct 4, 2012 at 0:44
  • Don't forget to clear the cache!
    – v-andrew
    Commented Oct 7, 2021 at 22:04

Better use .htaccess for this (if possible), no need to mess with the apache config files. Add these lines to the beginning of your .htaccess.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
  • 10
    It's definitely not better to use .htaccess because Apache ends up doing unnecessary work (like mapping URLs to filesystem paths) before performing the redirect as hinted at in point 1 here. (Also, the docs recommend against using mod_rewrite for simple redirection)
    – nickgrim
    Commented Aug 23, 2012 at 8:50
  • Nice point and useful information. My answer may still apply to those that don't have access to the apache config files however.
    – drcelus
    Commented Aug 23, 2012 at 11:17
  • Odd. Even this doesn't work. Something else is going on... In any case, I'd rather do with a conf command than this, but this is a good solution as a fall back plan. Commented Aug 24, 2012 at 3:28
  • What file do you add this to?
    – Richard
    Commented Jun 26, 2017 at 14:29

I had the same issue on apache 2.4 on centos 7 and the fix that worked for me without using the RewriteEngine was to add :443 to the ServerName directive under the VirtualHost for https.

DocumentRoot "/opt/www/example-docroot"
ServerName example.com:443

Our issue was config file precedence. In short, the HTTP VirtualHost in 000-default.conf was overriding the settings we had created in our own config file.

This is confusing to anyone who knows the meaning of the word "default" which means something only used if it's not defined elsewhere. We foolishly assumed that 000-default.conf would only provide configuration if it was not defined in another config file.

We were wrong. Apache just uses the config in the first file it encounters based on file name in alphanumeric order. It moves on to the next file if it cannot find a matching VirtualHost in that file, which causes fun confusion if you don't know how it works but is kind of neat once you do.

  • This is true, that's why I almost remove either this config or just use the default config to only host one page per container :)
    – djdomi
    Commented Jul 16, 2021 at 6:18

I think last time this happened to me it may have been something weird like NameVirtualHost *:80/NameVirtualHost *:443 and/or Listen 80/Listen 443 were somewhere other than ports.conf for apache2.

Here is what I have in VirtualHost to redirect to https for all pages except for press (had a video that couldn't be served https so had to redirect back to http otherwise people would get ugly warnings about the page being unsafe):

<VirtualHost *:80>
    ServerName example.com
    RewriteEngine On

    RewriteCond %{REQUEST_URI} !^/press/.*
    RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]

Clearing the cache of my browser fixed the issue.

You must log in to answer this question.

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