# Using Mod_Rewrite to hide IP address

## Ian

So my father is using namesdirect.com to manage a domain for our family, mostly for email.  I found out that I can make unlimited subdomains, which is awesome, except the only way to get them to work is either "stealth" forward (the URL is woloi.domain.com and never changes) or "standard" forward, which just redirects to a static IP and is what I'm doing now.

I'm looking for a way to rewrite my IP address to "hide" it from users.  So, user goes to http://woloi.domain.com, gets redirected to http://192.168.1.100.  Instead of having the IP address come up, I'd like to use Mod_Rewrite to replace with the domain name, so the user only sees http://woloi.domain.com/whatever/path/they/want.

I think this makes sense...if it's hopelessly confusing I'm sorry, I know what I need but I'm having trouble expressing it... :p

----------

## Hu

Why are you trying to hide the IP address?  The only way to completely prevent a user from knowing your address is to force their communications to go through an intermediate third party, and let that third party forward the request to you.  If you are just trying to hide it for the sake of not confusing the user, that may be possible.  Even so, I doubt mod_rewrite is what you want for that case.

----------

## Ian

I know I can't completely hide the IP address, that's...kind of impossible to run a server on the internet if you hide the IP address.  My reason is purely cosmetic, an IP address looks ugly, a nicely formatted domain name looks pretty.  Not to mention, for less technical users, if the URL suddenly changes to an IP address, they may become confused, especially since many phishing sites do just that.

I was under the impression that Mod_Rewrite can change the URL to be whatever you want, but I wasn't able to figure out if you could alter the domain name as well, or how to do that.  I'm not very good with regular expressions, so I may have just misunderstood the documentation.

----------

## Hu

As I understand it, mod_rewrite is for changing the URL coming in from the browser, so that you could map URLs to varying places on the filesystem based on rewrite expressions.  Your problem seems to be the inverse.  You want to rewrite outgoing URL references so that the client stays on the same domain name.

I think all you need is to have the pages on the server use relative href attributes, rather than absolute ones.  This also makes the page easier to maintain, since you do not need to do a global substitution if the server address changes.

<a href="http://192.168.1.100/my/other/directory.html">a bad link</a>

<a href="/my/other/directory.html">a good absolute link</a>

<a href="../other/directory.html">a good relative link</a>

----------

## Ian

Using relative versus absolute links has nothing to do with my problem right now.  This occurs even with just the apache directory browser (I forget which mod that is).

I thought mod_rewrite could also modify the URL being displayed, perhaps I'm wrong, but that's why I asked here.

----------

## djnauk

There is no (realistic) way that you can 'rewrite' the URL that visitors see (in their address bar) when the view your site. So long as you only have an IP address available to contact your server, only an IP address can be used in the address bar.

The use of mod_rewrite is server only and is designed to change URLs on-the-fly within the server to accomodate better URLs or changes in the underlying system. However, there is a sub-set of options which can be useful - mod_rewrite can be told to re-write the URL and then use that URL in a sub-request.

This has the advantage of re-writing the URL and contacting another server, which is what you want to do, but you must have a server which listens to your domain and will also mean that there will be two children used for every connection - the first to receive the request from the visitor and the second to forward it on to your server.

Another possible option, but again requires a third-party, is a forwarding proxy. You may be able to get away with setting up something like Squid to forward requests on to your system by hard-coding the IP address you need to the domain names you are using in the /etc/hosts file (bypassing any DNS lookup).

But, in all cases you will need a third-party to handle the requests for your domain and then pass them along to your server.

----------

## Hu

After reading djnauk's post and re-reading the original, I think that my answers were based on a completely incorrect understanding of the problem.  Ian was being quite literal when he said the URL never changes.  Most likely, that case is implemented using a frame at that domain, and the subframe is pointed to the IP address.  Since the browser only shows the URL of the master frame, that accounts for the "never changes" part of the original condition.  If this new interpretation is correct, I am not aware of any way to resolve this that does not involve getting a host with support for properly assigning a DNS A record (IPv4) or DNS AAAA record (IPv6) to an IP address of Ian's choosing.

----------

