How Do I Use Apache RewriteMap with PHP?
<p>I've been looking into using <a href="https://httpd.apache.org/docs/current/rewrite/rewritemap.html" rel="nofollow">RewriteMap</a> to dynamically handle a range of URL rewrites, and what I'd like to do is use a PHP script to generate the appropriate rewrite conditions and rules -- but I just can't get it working.</p>

<p>I've got Apache running on a Linux server with WebHost Manager and cPanel.</p>

<p>I added the following to WHM > Apache Configuration > Include Editor > PreVirtual Host Include > All Versions:</p>

<pre><code>RewriteMap myrules "prg:/usr/lib/php /path/to/my/script.php"
</code></pre>

<p>I updated the config file and Apache restarted okay.</p>

<p>Now I'd like to call the RewriteMap I defined from an .htaccess file to execute the rewrite, but this is where I'm stuck and everything seems to break.</p>

<p>I'd like to grab the incoming domain, check it against the map, and return the associated domain:user_name value. Something like:</p>

<pre><code>RewriteCond %{HTTP_HOST} ^(www\.)?${myrules:%{HTTP_HOST}}\.com$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/user/${myrules:%{HTTP_HOST}}/$1 [NC,P,L]
</code></pre>

<p>So, just for example, if my map has johnsmith.com:john-smith, then the incoming URL johnsmith.com would proxy for the content at mywebsite.com/user/john-smith/.</p>

<p><strong>First, I'm pretty sure the cond/rule above is all wrong.</strong></p>

<p>Getting it correct is my first priority.</p>

<p><strong>Second, how would I output my map using the PHP script defined by the RewriteMap?</strong></p>

<p>Any help would be appreciated!</p>