Linux – Redirect IP to a domain name using htaccess

.htaccessapache-2.2linuxUbuntu

Let's say I have this IP Address 11.12.13.14 and the domain example.com. Now what I want is to redirect the user from IP Address to domain name (but without changing the domain name to address bar). So when the user requests 11.12.13.14/test it should open exapmle.com/test but not to redirect to domain name, in the address bar it should still remain 11.12.13.14/test.

I have seen this question Redirect to other domain but keep typed domain. I don't know if it works because I haven't tested it, but I suppose it does.

I am using Ubuntu 14.04 with Apache, so is there any wat to achieve this?

Here is what I have tried

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^ 11.12.13.14$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,NE,P]

Best Answer

You cannot really redirect without changing an address displayed in bar. If you want a similar effect, you have at least two options:

  1. reverse proxy (mod_proxy_http) - possibly transparent to user;
  2. HTML frame - obsolete, easy to detect and prone to fail with modern browsers.

Edit #1: For proxy solution, you need to place the directives below in global or VirtualHost (IP-based) section:

<IfModule mod_proxy_http.c>
    ProxyPass "/" "http://example.com/"
    ProxyPassReverse "/" "http://example.com/"
<IfModule>
Related Topic