1

I'm trying to redirect multiple VirtualHosts without a preceeding "www." from one VirtualHost to another, like so:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias example.org
    ServerAlias example.net
    # I either need some kind of Apache conditional here...
    Redirect 301 / http://www.example.com/
    # ... or I need to substitute the Redirect *URL* with a variable
    # for the ServerName/ServerAlias
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com
</VirtualHost>

I could just create several VirtualHost containers and redirect each one individually, but I prefer not to repeat my code, if possible.

I'm using Apache 2.2 and I prefer not to use mod_rewrite to redirect. I'm fair with Apache, but far from an expert.

1
  • This cannot be accomplished using mod_alias, because it does not support conditionals or %{variables}. I believe mod_rewrite is necessary to do this. Feb 1, 2019 at 1:51

3 Answers 3

1

I wrote the following condition that I'm using in an .htaccess file for redirecting any domain without the "www", this work for any domain name. The domain look could be done more comprehensive, for instance I only look for number, letters and underscore in the domain name since I know all the domains where this rule will apply will have just that, but you could easily replace the regular expression with a more complex one if you need.

  RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9-_]*)\.([a-zA-Z0-9]{2,3})$ [NC]
  RewriteRule ^(.*)$ http://www.%1.%2/$3 [L,R=301]
2
  • Thanks for the effort. I'm trying to avoid mod_rewrite.
    – Jeff
    Oct 16, 2012 at 17:53
  • Sure, too bad you can't use the above conditions in your virtual host definition.
    – Flupkear
    Oct 16, 2012 at 18:21
1

The solution I used isn't "Don't Repeat Yourself" friendly, but it works and helps me achieve my preference of not using mod_rewrite. Any better solutions and I'll happily mark your answer right.

<VirtualHost *:80>
    ServerName example.com
    Redirect 301 / http://www.example.com/
</VirtualHost>

<VirtualHost *:80>
    ServerName example.org
    Redirect 301 / http://www.example.org/
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com
    ServerName www.example.org
</VirtualHost>
0

assuming they all going to one virtual host, i'd do this:

<VirtualHost *:80>
   ServerName _default_:80
   Redirect / http://www.example.com/
</VirtualHost>

<VirtualHost *:80>
   ServerName www.example.com
   DocumentRoot /var/www/html/
</VirtualHost>

* UPDATE *

Maybe this will help you? Apache 2.4 module mod_macro 1.2.1

3
  • No, they're each going to their respective www.example.tld on the same VirtualHost.
    – Jeff
    Oct 16, 2012 at 19:32
  • Appreciate that! Unfortunately I'm stuck with Apache 2.2 for now. I believe > 2.3 has conditionals.
    – Jeff
    Oct 16, 2012 at 19:42
  • The current version for Apache 2.4 is 1.2.1. For Apache 2.2, use 1.1.11. For Apache 2.0, use 1.1.6. It won't work with Apache 1.3 for which you should use version 1.1.2.
    – alexus
    Oct 16, 2012 at 19:59

You must log in to answer this question.

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