I've been looking into using RewriteMap 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.
I've got Apache running on a Linux server with WebHost Manager and cPanel.
I added the following to WHM > Apache Configuration > Include Editor > PreVirtual Host Include > All Versions:
RewriteMap myrules "prg:/usr/lib/php /path/to/my/script.php"
I updated the config file and Apache restarted okay.
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.
I'd like to grab the incoming domain, check it against the map, and return the associated domain:user_name value. Something like:
RewriteCond %{HTTP_HOST} ^(www\.)?${myrules:%{HTTP_HOST}}\.com$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/user/${myrules:%{HTTP_HOST}}/$1 [NC,P,L]
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/.
First, I'm pretty sure the cond/rule above is all wrong.
Getting it correct is my first priority.
Second, how would I output my map using the PHP script defined by the RewriteMap?
Any help would be appreciated!
HTTP_HOST
is your server's host name, not the client's name. There are someREMOTE_*
variables, though I am not sure how reliable these values are. – Olaf Dietsche