Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I know how to use Apache's mod_rewrite in a common way but now I'm trying to use it in a different way which was not a success to this moment. Here's what I wrote:

RewriteEngine on
RewriteMap lowercase int:tolower
RewriteCond %{lowercase:%{HTTP_HOST}} ^([^.]+)\.internal$
RewriteRule ^(.*)$ http://%1.local$1

And here's the whole story:

I have added two lines to my hosts file like so:

127.0.0.1    test.local
127.0.0.1    test.internal

I've also added a virtual host for test.local in my Apache and created a file so the URL http://test.local/info.php prints out PHP's configuration and stuff. Now let's get back to mod_rewrite directives; I was hoping by writing what I wrote at the begging of the post, when I open the URL http://test.internal/info.php it will be served by test.local/info.php script. Can anyone tell me what did I do wrong?

BTW, I've written the given directives in Apache's main configuration file, not in a virtual host's and I insists on this.

[UPDATE]

I've added the following two lines to Apache's configuration file so I can debug the Rewrite's behaviour:

RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9

I have couple of virtual hosts on this machine which use Rewrite and when I visit them, due to these two lines a log file is created in the specified path. But when I visit http://test.internal/info.php no log file is created.

[UPDATE]

I'm going to clarify my aim maybe I'm doing everything wrong from scratch.

I have numerous VPSs each hosting a different website. And they all reside in a local network and I want to add a frontend to this collection of website so they can be accessed from the Internet. But the thing is that members of this collection are dynamically added or removed so I don't want to modify my DSN and/or reverse proxy server's config file each time something's changed. I can use a wildcard in DNS server to direct all of requests to a specific domain to the frontend (the reverse proxy server) and then it will be up to the frontend to redirect the request to the appropriate VPS using their subdomain (each subdomain is assigned to a VPS logically). To this end I will setup a local DNS mapping some dummy domain names to internal VPS IP's. All that remains is that reverse proxy server should rewrite the request's URL so it matches the dummy domains.

The only part of this solution that I can't grasp is how to tell Apache to map the incoming requests' domain to dummy ones!

Any suggestions?

share|improve this question
2  
Often, if you enable logging of mod_rewrite activity via the RewriteLog directive these problems become much easier to debug. –  larsks Aug 7 '13 at 12:50
    
Thanks I will look into it and update the question if I could discover anything new. –  Mehran Aug 7 '13 at 12:53
    
If you have these rewrite rules in the global configuration then you'll need to put RewriteOptions Inherit in any VirtualHost directives for them to inherit the rules. –  Chris S Aug 7 '13 at 13:28
    
That's the point, I don't want these rules in any of the virtual hosts. I just want the incoming URL to be rewritten. Much like it happens in mod_proxy but this time the domain part is matched and rewritten. Later I will leave no virtual host on this instance of Apache and take them all away to some other machine. This one will only play a reverse proxy role. –  Mehran Aug 7 '13 at 13:37
    
Are you following the instructions on Dynamic mass virtual hosts with mod_rewrite? If so, did you see their document on why that's generally a bad idea unless necessary (for instance, if you can only edit .htaccess)? Why don't you want to configure virtual hosts on the reverse proxy? –  Paulo Almeida Aug 7 '13 at 14:56

1 Answer 1

up vote 0 down vote accepted

I found another way to solve my problem, it's called mod_proxy_express and it's available in Apache 2.4. It's capable of mapping a mass number of domains to their IPs using a database.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.