0

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 ServerNames without multiple VirtualHosts). 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? :)

2 Answers 2

1

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

RewriteRule ^www\.([^.]+)\.com(.*) /var/www/html/$1$2 [L]

You want

RewriteRule ^www\.([^.]+)\.com(:[0-9]+)?/(.*) /var/www/html/$1/$3 [L]
1
  • I thought %{HTTP_HOST} wouldn't bring the port along. Let me try it... Nope. Didn't work.
    – cregox
    May 6, 2011 at 5:15
0

As I said I would, here's what I did to solve my issue:

UseCanonicalNames off

NameVirtualHost *:8888
<VirtualHost *:8888>
  ServerName example1.com
  ServerAlias *.example1.com

  # more stuff below just to illustrate why I don't like this solution
  # redirect to HTTPS
  RewriteEngine on
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^login\.(.*)$
  RewriteRule ^(.*) https://%1/login$1 [L]
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^www\.([^.]+\.com)$
  RewriteRule ^/login(.*) https://%1/login$1 [L]
</VirtualHost>

<VirtualHost *:8888>
  ServerAlias *
  VirtualDocumentRoot /var/www/html/%-3

  # more stuff below again, to illustrate why I don't like this solution
  # redirect to HTTPS
  RewriteEngine on
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^login\.(.*)$
  RewriteRule ^(.*) https://%1/login$1 [L]
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^www\.([^.]+\.com)$
  RewriteRule ^/login(.*) https://%1/login$1 [L]
</VirtualHost>

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .