1

I have the following wildcard vHost:

<VirtualHost *:80>
    ServerAdmin hostmaster@example.de
    ServerName autodiscover.*.*
    ServerAlias autoconfig.*.*

    RewriteEngine On 
    RewriteCond %{HTTPS} !=on 
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</VirtualHost>

But my RewriteRule is wrong.

For Example:

autoconfig.testdomain.de gets mapped to https://autoconfig.testdomain.de

What I want:

  • autoconfig.*.* gets mapped to https://autoconfig.maindomain.de

  • autodiscover.*.* gets mapped to https://autodiscover.maindomain.de

I want to parse only the subdomain part in %{HTTP_HOST} to subdomain.example.de, but only if the subdomain part is either autoconfig or autodiscover

MrWhite
  • 12,647
  • 4
  • 29
  • 41
Tim Altgeld
  • 49
  • 1
  • 7

1 Answers1

1

Try the following instead:

RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteRule ^/(.*) https://%1.maindomain.de/$1 [R=301,L]

There's no need to check %{HTTPS} !=on since you are in the vHost for port 80.

The RewriteCond directive captures just the subdomain from the requested hostname (which can only be autoconfig or autodiscover from the vHost config stated). This is then referenced using the %1 backreference in the RewriteRule substitution string.

Alternatively, you can explicitly check for only the autoconfig or autodiscover subdomains in the condition:

RewriteCond %{HTTP_HOST} ^(autoconfig|autodiscover)\.

You need to clear your browser cache before testing, since the erroneous 301 (permanent) redirects will have been cached by the browser. Test with 302 (temporary) redirects to avoid caching issues.

ServerName autodiscover.*.*

I guess you must be on an older version of Apache, since you can't use wildcards in the ServerName directive on newer versions of Apache (it would otherwise create an ambiguity, since the ServerName is used to create self-referenctial URLs in certain scenarios). You should only specify wildcards in the ServerAlias directive.

This should be changed to something like:

ServerName autodiscover.something.something
ServerAlias autodiscover.*.*
ServerAlias autoconfig.*.*
MrWhite
  • 12,647
  • 4
  • 29
  • 41