I've got this setup that works almost all right and, apparently, works the exact same way than this:
UseCanonicalNames off
<VirtualHost *:8888>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example1\.com
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^www\.([^.]+)\.com(.*) /var/www/html/$1$2 [L]
</VirtualHost>
What it does is redirecting everything www.NNN.com
except example1
to their respective folder, inside /var/www/html/NNN
. That's the basics of what I need. It has the added bonus of allowins a name (example1
) to host all other names within itself at example1/NNN/
, which is also working fine!
The main issue, maybe, is it's all configured behind an Amazon ELB directing port 80 to internal 8888.
So, the only problem I've detected so far is: if I point the browser to www.example.com/anything/
it's just fine, but to www.example.com/anything
(without the last slash) it will redirect the browser to a broken link www.example.com:8888/anything/
be it with example
or NNN
.
Now, I do realize I probably shouldn't be using Rewrite for this, and I'm already trying to change everything to mod_vhost_alias instead but it kept me wondering if there's anything that could be done to fix the problem above along with any further issue that might raise for I was not using the "proper way" with UseCanonicalNames on
and ServerName
for obvious reasons (I don't think we can set multiple ServerName
s without multiple VirtualHost
s). Of course, using them would be ideal, but I couldn't find any way and thus why I'm going the other direction now.
So, anyone steps forward? :)
First, your are right in that this is a pretty significant abuse of mod_rewrite.
That being said, the problem you are facing here has to do with your rewrite rule. It looks like you are capturing everything after the hostname when really all you want to capture is the path. Accordingly instead of
You want
As I said I would, here's what I did to solve my issue:
While I wait and hope for an answer on the question, this is my accepted solution.
I actually like it, almost more than the one I was trying with Rewrite - plus this is more guarantee to work in other cases. Maybe I should just ask another question on how can we macro / template / function / do something with that duplicated code to write it just once.