title: Outlook autodiscover with HTTP redirect does not work
body: <p>I am trying to configure autodiscover service throught HTTP 302 redirect method which redirects to a domain which correctly provides autodiscover.xml via HTTPS. With this configuration POP/IMAP/SMTP and EAS autodiscover settings are correctly provided on iPhone and Android so I believe the service is configured correctly and the XML payload is correct. Moreover also <a href="https://testconnectivity.microsoft.com/" rel="nofollow noreferrer">https://testconnectivity.microsoft.com/</a> gives positive results on both Outlook Autodiscover ed Exchange ActiveSync Autodiscover.</p>
  
  <p>The problem is happening only with Outlook for Office 365 and Windows App Mail (on windows 10). In both cases, after filling the email address in the registration form, the request to the autodiscover goes fine (I added a printout of the generated XML and it is correct). However, there is a second screen where it is asked to insert the password and here is the problem. Basically a new GET request is issued (/AutoDiscover/AutoDiscover.xml) to the original domain (the one over HTTP) but the 302 redirect in this case does not happen and a new window appears saying the parameters were not been found.
  It's like the second call after the password input would expect an answer different from a 302 redirect.</p>
  
  <p>To better describe my configuration:</p>
  
  <ul>
  <li>email address for which I request autodiscover: user@domain1.com</li>
  <li>configuration of DNS: autodiscover.domain1.com CNAME to autodiscover.domain2.com</li>
  <li>autodiscover.domain1.com (IP of autodiscover.domain2.com through CNAME in the DNS) answers only via HTTP and does a 302 redirect to <a href="https://autodiscover.domain2.com/AutoDiscover/AutoDiscover.xml" rel="nofollow noreferrer">https://autodiscover.domain2.com/AutoDiscover/AutoDiscover.xml</a> (case insensitive)</li>
  <li>autodiscover.domain2.com has an Apache rule so that the file AutoDiscover.xml (case insensitive) is generated by a PHP script which analyze the content of the request and answers with the proper XML for EAS or Outlook based on request content.</li>
  </ul>
  
  <p>I also tried to set an SRV record pointing directly autodiscover.domain2.com but the result is the same.</p>
  
  <p>I am pretty confident the XML payload is correct because on Android and iPhone the EAS is configured fine, it's only with Outlook and App Mail that I cannot manage to make it work.</p>
  
  <p>Any help is appreciated!</p>
  
  <p>This is the configuration of the virtualhost:</p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
      ServerName autodiscover.domain2.com
      ServerAlias autodiscover.*
      ServerAlias autoconfig.*
      DocumentRoot /var/www/autodiscover.domain2.com/public_html
      ErrorLog /var/log/autodiscover.domain2.com_error_log
      CustomLog /var/log/autodiscover.domain2.com_access_log combined
  &lt;/VirtualHost&gt;
  
  &lt;VirtualHost *:443&gt;
      ServerName atodiscover.domain2.com
      ServerAlias autoconfig.domain2.com
      DocumentRoot /var/www/autodiscover.domain2.com/public_html
      ErrorLog /var/log/autodiscover.domain2.com_error_log
      CustomLog /var/log/autodiscover.domain2.com_access_log combined
  
      ScriptAlias /AutoDiscover/AutoDiscover.xml /var/www/autodiscover.domain2.com/public_html/outlook.php
      ScriptAlias /Autodiscover/Autodiscover.xml /var/www/autodiscover.domain2.com/public_html/outlook.php
      ScriptAlias /autodiscover/autodiscover.xml /var/www/autodiscover.domain2.com/public_html/outlook.php
  
      SSLEngine on
      SSLCertificateFile /path/to/ssl.cert
      SSLCertificateKeyFile /path/to/ssl.key
      SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>this is the .htaccess:</p>
  
  <pre><code>RewriteEngine On
  
  # thunderbird
  RewriteRule ^.*config-v1\.1\.xml$ https://autodiscover.domain2.com/thunderbird.php [NC,L]
  
  # redirect autodiscover.xml to https of domain2 (302)
  RewriteCond %{HTTPS} off
  RewriteRule ^.*autodiscover\.xml$ https://autodiscover.domain2.com%{REQUEST_URI} [NC,L,R=302]
  
  # redirect autodiscover.xml to outlook.php
  RewriteCond %{HTTP_HOST} ^autodiscover\.domain2\.com$ [NC]
  RewriteRule ^.*autodiscover\.xml$ https://autodiscover.domain2.com/outlook.php [NC,L]
  
  # redirect autodiscover.json to outlook.json.php
  RewriteCond %{HTTP_HOST} ^autodiscover\.domain2\.com$ [NC]
  RewriteRule ^.*autodiscover\.json$ https://autodiscover.domain2.com/outlook.json.php [NC,L]
  </code></pre>
  
  <p>this is the outlook.php script placed in /var/www/html/autodiscover.domain2.com/public_html/:</p>
  
  <pre><code>&lt;?php
  header("Content-Type: application/xml");
  $data = file_get_contents("php://input");
  preg_match("/\&lt;EMailAddress\&gt;(.*?)\&lt;\/EMailAddress\&gt;/", $data, $matches);
  if($matches == false || count($matches)!=2) exit(0);
  $ar = explode('@' ,$matches[1]);
  if(count($ar)!=2) exit(0);
  $account = $ar[0];
  $domain = $ar[1];
  $email = $matches[1];
  $provider = 'domain2.com';
  
  $buf = '';
  
  if(preg_match('/mobilesync/',$data))
  {
      $buf = '&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006"&gt;
    &lt;Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006"&gt;
      &lt;Culture&gt;en:us&lt;/Culture&gt;
      &lt;User&gt;
        &lt;DisplayName&gt;'.$email.'&lt;/DisplayName&gt;
        &lt;EMailAddress&gt;'.$email.'&lt;/EMailAddress&gt;
      &lt;/User&gt;
      &lt;Action&gt;
        &lt;Settings&gt;
          &lt;Server&gt;
            &lt;Type&gt;MobileSync&lt;/Type&gt;
            &lt;Url&gt;https://eas.domain2.com/Microsoft-Server-ActiveSync&lt;/Url&gt;
            &lt;Name&gt;https://eas.domain2.com/Microsoft-Server-ActiveSync&lt;/Name&gt;
          &lt;/Server&gt;
        &lt;/Settings&gt;
      &lt;/Action&gt;
    &lt;/Response&gt;
  &lt;/Autodiscover&gt;';
  }
  else
  {
      $buf = '&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006"&gt;
   &lt;Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a"&gt;
    &lt;User&gt;
     &lt;DisplayName&gt;'.$email.'&lt;/DisplayName&gt;
    &lt;/User&gt;
    &lt;Account&gt;
     &lt;AccountType&gt;email&lt;/AccountType&gt;
     &lt;Action&gt;settings&lt;/Action&gt;
      &lt;Protocol&gt;
       &lt;Type&gt;POP3&lt;/Type&gt;
       &lt;Server&gt;pop.'.$provider.'&lt;/Server&gt;
       &lt;Port&gt;995&lt;/Port&gt;
       &lt;LoginName&gt;'.$email.'&lt;/LoginName&gt;
       &lt;DomainRequired&gt;off&lt;/DomainRequired&gt;
       &lt;SPA&gt;off&lt;/SPA&gt;
       &lt;SSL&gt;on&lt;/SSL&gt;
       &lt;AuthRequired&gt;on&lt;/AuthRequired&gt;
       &lt;DomainRequired&gt;off&lt;/DomainRequired&gt;
      &lt;/Protocol&gt;
      &lt;Protocol&gt;
       &lt;Type&gt;IMAP&lt;/Type&gt;
       &lt;Server&gt;imap.'.$provider.'&lt;/Server&gt;
       &lt;Port&gt;993&lt;/Port&gt;
       &lt;DomainRequired&gt;off&lt;/DomainRequired&gt;
       &lt;LoginName&gt;'.$email.'&lt;/LoginName&gt;
       &lt;SPA&gt;off&lt;/SPA&gt;
       &lt;SSL&gt;on&lt;/SSL&gt;
       &lt;AuthRequired&gt;on&lt;/AuthRequired&gt;
      &lt;/Protocol&gt;
      &lt;Protocol&gt;
       &lt;Type&gt;SMTP&lt;/Type&gt;
       &lt;Server&gt;smtp.'.$provider.'&lt;/Server&gt;
       &lt;Port&gt;465&lt;/Port&gt;
       &lt;DomainRequired&gt;off&lt;/DomainRequired&gt;
       &lt;LoginName&gt;'.$email.'&lt;/LoginName&gt;
       &lt;SPA&gt;off&lt;/SPA&gt;
       &lt;Encryption&gt;SSL&lt;/Encryption&gt;
       &lt;AuthRequired&gt;on&lt;/AuthRequired&gt;
       &lt;UsePOPAuth&gt;off&lt;/UsePOPAuth&gt;
       &lt;SMTPLast&gt;off&lt;/SMTPLast&gt;
      &lt;/Protocol&gt;
     &lt;/Account&gt;
   &lt;/Response&gt;
  &lt;/Autodiscover&gt;';
  }
  echo $buf;
  </code></pre>
  
  <p>this is the outlook.json.php:</p>
  
  <pre><code>&lt;?php
  $url = 'https://eas.domain2.com/Microsoft-Server-ActiveSync';
  
  header('Content-type: application/json');
  if (strtolower($_GET['Protocol']) == 'activesync') {
    echo '{"Protocol":"ActiveSync","Url":"' . $url . '"}';
  }
  elseif (strtolower($_GET['Protocol']) == 'autodiscoverv1') {
    echo '{"Protocol":"AutodiscoverV1","Url":"https://' . $_SERVER['HTTP_HOST'] . '/Autodiscover/Autodiscover.xml"}';
  }
  else {
    http_response_code(400);
    echo '{"ErrorCode":"InvalidProtocol","ErrorMessage":"The given protocol value \u0027' . $_GET['Protocol'] . '\u0027 is invalid. Supported values are \u0027ActiveSync,AutodiscoverV1\u0027"}';
  }
  </code></pre>