I'd like to do the following: using Apache, redirect all of the following URLs:


to the same place: https://beta.example.com. I'm close, but not quite there. Here's what I've got:

NameVirtualHost xxx.xxx.xxx.xxx:80
NameVirtualHost xxx.xxx.xxx.xxx:443

### rewrite scheme via http://stackoverflow.com/questions/1100343/apache-redirect-from-non-www-to-www                                    
<VirtualHost xxx.xxx.xxx.xxx:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/example
    CustomLog /var/log/httpd/example-log combined
    ErrorLog /var/log/httpd/example-log

<Directory /var/www/html/example>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ https://beta.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    Options -Indexes
    Options +FollowSymLinks
    DirectoryIndex index.php

<VirtualHost xxx.xxx.xxx.xxx:80>
    ServerName beta.example.com
    DocumentRoot /var/www/html/example-beta
    CustomLog /var/log/httpd/example-beta-log combined
    ErrorLog /var/log/httpd/example-beta-log

    <IfModule mod_rewrite.c>
              RewriteEngine on
              # redirect all http -> https                                                                                             
              RewriteCond %{HTTPS} off
              RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

<VirtualHost xxx.xxx.xxx.xxx:443>
    ServerName beta.example.com
    DocumentRoot /var/www/html/example-beta
    ErrorLog /var/log/httpd/example-beta-log
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\" %{SSL_PROTOCOL}x %{SSL_CIPHER}x" combined_ssl
    CustomLog /var/log/httpd/example-beta-log combined_ssl

    SSLEngine on
    ### SSL details omitted for clarity.

What I get from this:

http://example.com => https://beta.example.com
http://www.example.com => Content for www.example.com (/var/www/html/example)
http://beta.example.com => https://beta.example.com
https://example.com => https://example.com (correct content but wrong url)
https://www.example.com => https://www.example.com (correct content but wrong url)
https://beta.example.com => https://beta.example.com

It's probably worth noting that my ssl certificate is fully wildcard-able, so I should be good there, and all the SSL stuff seems to be working (even when the pages are being served up under https://example.com). Meanwhile, there are no .htaccess files in place -- I'm trying to do everything through apache config.

So the problem is getting https://example.com to redirect correctly, with and without the www prefix. Any advice out there? Thanks!

  • Is the space in ServerAlias www. example.com an error in pasting? – DerfK Jun 14 '16 at 15:29
  • oops / yes, an error in pasting / corrected in original / thx – Jim Miller Jun 14 '16 at 15:40

From what I'm reading, you would need two separate VirtualHost entries, one for HTTP, and one for HTTPS. These are different by the port (:80 for HTTP and :443 for HTTPS), and also because you have the SSL On section for HTTPS. Each would need to have all names listed:

ServerName  beta.example.com
ServerAlias example.com www.example.com

Then, you would have all the same DocumentRoot and other information listed there. For the HTTP VirtualHost, you already know that you need to redirect, because you know you want HTTPS, so you only need the RewriteRule:

RewriteRule ^ https://beta.example.com%{REQUEST_URI} [R=301,L]

For the HTTPS VirtualHost, you would need the same RewriteRule, but you would also need to add a condition:

RewriteCond %{HTTP_HOST} !^beta\.example\.com$
RewriteRule ^ https://beta.example.com%{REQUEST_URI} [R=301,L]

The RewriteCond is there to prevent an unending redirect.

This would allow you to control the redirects in a single place. If you don't have some reason to make a VirtualHost for each domain, then it's just simpler to have one.

  • This is very close! but some questions: (1) Should I return 302 or 301? (2) I think those "http://" in your rewrite rules above should be https://? (3) example.com and example.com are being handled with https, but their URLs are not getting changed to beta.example.com -- they stay at example.com or www.example.com. Thoughts? (and thanks!) – Jim Miller Jun 14 '16 at 19:08
  • Yes, that should have been https://. I was testing on my own server, and didn't have an SSL there to test with, and forgot to change that. 301s are permanent, so you can use that, and I updated it, but with "beta" in the name, I had suspected a temporary (302) redirect. The rules did work for me, so I can't replicate it not working. However, one gotcha with SSL is that you will need to accept the SSL before the redirect is served. I changed the rewrite to ^, which I have read is supposed to always match, rather than (.*), which may fail. – DKing Jun 14 '16 at 20:54
  • Thanks much! I'm traveling at the moment but I'll check it out and report back. – Jim Miller Jun 14 '16 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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