As you say with hosts
you can control only the IP, but since the new URL is another subdomain we can combine using hosts
with a local web server for rewriting the URL. Note that this workaround requires the application use a browser or another component that follow URL redirections.
I'm going to use a vanilla installation of Apache since we don't need PHP or a DB, but if you want you can use a distribution like XAMPP. The steps are:
Download the Apache ZIP from Apachehaus, I the used VC11 32 bit version. Note that this version needs the Visual C++ 2012 redistributable, download and install it you don't have it already installed (if you use another version you would need the matching version of the C++ Redistributable).
Extract the contents in the ZIP in a folder, I'm going to use C:\Apache
but you can use another folder, just use that folder in the next steps.
Go to C:\Apache\Apache24\conf
and open httpd.conf
with a text editor and made the following changes:
- Search for
Define SRVROOT "/Apache24"
and replace it with Define SRVROOT "C:\Apache\Apache24"
, this tells Apache where it's installed.
- Search for
#LoadModule rewrite_module modules/mod_rewrite.so
and delete the initial #
, this loads the URL rewriting module.
- Search for
DocumentRoot "${SRVROOT}/htdocs"
, a few lines after you will see AllowOverride None
, replace it with AllowOverride All
, this allows URL rewriting.
You can start Apache manually going to C:\Apache\Apache24\bin
and executing httpd.exe
, but its more convenient install it as a service. To do that open a cmd.exe
, go to that folder, and execute httpd.exe -k install
. Once the service is installed you can start/stop it from the Services administrative tool of the Control Panel (you need to start it the first time after installing from there, then it will be started automatically with Windows).
Test that the server is running, open http://localhost
, it would show a welcome page if is working properly. If the server don't start launch it manually from the command line, it will print the error that prevent it starting (for example if you put an incorrect path in ServerRoot).
Create a new file in your text editor and save it as .htaccess
(notice the leading dot) in the folder C:\Apache\Apache24\htdocs
, you have to put the following in that file (change the domain and subfolder to the real ones):
RewriteEngine on
RewriteRule ^sub1/sub2/sub3/(.*)$ http://subdomain2.domain.com/sub4/$1 [R]
Finally, edit the Windows hosts
file and add the following line:
127.0.0.1 subdomain1.domain.com
You can test if the redirection works from a browser, if all went OK, now when you type http://subdomain1.domain.com/sub1/sub2/sub3/[anything] the browser will be redirected to http://subdomain2.domain.com/sub4/[anything].
If want to use this workaround in several computers you don't need to install Apache in each one, supposing than 192.168.0.10
is the IP of the machine with Apache just add in the hosts
file of each machine:
192.168.0.10 subdomain1.domain.com
Note that you need to allow connections to Apache in the firewall, at least to the local network.
hosts
file in each computer). I'm going to make some tests and I'll post an answer if my idea works.