Operating System & Version
CENTOS 7.8
cPanel & WHM Version
v90.0.10

hipwebdesign

Registered
Nov 11, 2019
4
1
1
Bellingham, WA
cPanel Access Level
Website Owner
I have a RewriteMap `prg` in the Pre VirtualHost Include (pre_virtualhost_global.conf) like this:

Code:
RewriteMap livemap "prg:/home/USER/applications/DOMAIN/public/rewriteMap.php"
The script listens on php://stdin for the domain, queries my database for a match, and then returns the rewrite path, like this:

PHP:
#!/usr/local/bin/php
<?php

/* DB query here, build $map... */

// Listen for input

$stdout = fopen('php://stdout', 'w');
$stdin  = fopen('php://stdin', 'r');
stream_set_write_buffer($stdout, 0);

while(!feof($stdin)):
    
    $input  = rtrim(fgets($stdin), "\n");
    
    fputs($stdout, (array_key_exists($input,$map) ? $map[$input] : 'NULL') . "\n");
    
endwhile;

exit;

?>
Then, in my .htaccess file, I use this map to make my RewriteRules, like this:

Code:
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
  RewriteRule ^ - [E=MAPTO:${livemap:%1}]
  RewriteCond %{ENV:MAPTO} !=""
  RewriteRule ^.*$ https://mywebsite.com/%{ENV:MAPTO}/$0 [P,NC]
</IfModule>
This all works as expected unless I use HTTPS.

For example, if I visit http://myaliaswebsite.com, my RewriteMap will return the DB result and .htaccess will set the MAPTO env variable with its value, allowing me to proxy the appropriate content on mywebsite.com.

However, if I use https://myaliaswebsite.com, the RewriteMap doesn't return a result — it doesn't apepar to receive any input at all — and no value is stored in MAPTO, thus failing the RewriteCond.

I can't figure out why this is working over HTTP and not HTTPS.

The primary domain and the alias are on the same SSL cert. I used LogLevel rewrite:trace8 to tail the rewrites and I can see it's applying the same rules over HTTPS, but not returning a MAPTO value whereas HTTP will.
 

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,304
1,252
313
Houston
This seems as though it would be best suited in the domain's VirtualHost include Modify Apache Virtual Hosts with Include Files | cPanel & WHM Documentation

Also, I believe when using HTTP_HOST you specify that the request has to come from HTTP rather than HTTPS. In order for this to work when coming FROM https:// you'd need to use HTTPS_HOST in addition to HTTP_HOST in the rule using [OR] as defined in the mod_rewrite documentation

http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritemap said:
You can also set special flags for CondPattern by appending [flags] as the third argument to the RewriteCond directive, where flags is a comma-separated list of any of the following flags:

  • 'nocase|NC' (no case)
    This makes the test case-insensitive - differences between 'A-Z' and 'a-z' are ignored, both in the expanded TestString and the CondPattern. This flag is effective only for comparisons between TestString and CondPattern. It has no effect on filesystem and subrequest checks.
  • 'ornext|OR' (or next condition)
    Use this to combine rule conditions with a local OR instead of the implicit AND. Typical example:
  • Code:
    RewriteCond "%{REMOTE_HOST}" "^host1" [OR]
    RewriteCond "%{REMOTE_HOST}" "^host2" [OR]
    RewriteCond "%{REMOTE_HOST}" "^host3"
    RewriteRule ...some special stuff for any of these hosts...
    Without this flag you would have to write the condition/rule pair three times.
  • 'novary|NV' (no vary)
    If a HTTP header is used in the condition, this flag prevents this header from being added to the Vary header of the response.
    Using this flag might break proper caching of the response if the representation of this response varies on the value of this header. So this flag should be only used if the meaning of the Vary header is well understood.