3

I have a piece of legacy software that will not be updated and I have no ability to modify, that calls a specific URL for information. However, that URL has subsequently changed and no longer returns a working result. Heres the example: http://subdomain1.domain.com/sub1/sub2/sub3/[NUMBER]

Their current structure is: http://subdomain2.domain.com/sub4/[NUMBER]

As I understand it, HOSTS is IP based only. Is there a way for me to force anything calling the first example to return the second example?

Thanks

5
  • The application uses that URL's to access a web page and show it in an integrated browser or to retrieve custom data from there? Commented Sep 21, 2017 at 18:05
  • it uses an integrated browser, based on the (frequent) errors I receive for other things, I believe it's based off of IE6.
    – Kevin O.
    Commented Sep 21, 2017 at 18:07
  • Then I think I have an idea, but first: installing an Apache web server in your machine for this would be overkill? It wouldn't take a lot of resources and you could start/stop it as needed. Commented Sep 21, 2017 at 18:14
  • I'm not the only one who uses this tool and would like to deploy a solution if possible to other members of my team who, unfortunately, are less technically skilled than I am. Ideally we'd like a set-and-forget solution, but if one's not available, we'll continue jury-rigging it through other windows
    – Kevin O.
    Commented Sep 21, 2017 at 18:18
  • Well, you don't need the server installed in each computer, you can install the server in a computer in your local network that all the people can access (but you need to modify the hosts file in each computer). I'm going to make some tests and I'll post an answer if my idea works. Commented Sep 21, 2017 at 18:25

2 Answers 2

3

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.

8
  • 1
    +1 Darn, beat me to it. =) As an FYI, another great place for no-frills Windows builds of Apache is Apache Lounge. They have the benefit of building the latest official versions of Apache regularly with both older and new versions of Visual Studio. Commented Sep 22, 2017 at 6:48
  • Also, I had better luck with RewriteRule ^(.*)sub1/sub2/sub3 (though this could have been user error. =P) Commented Sep 22, 2017 at 9:11
  • @Anaksunaman Thanks for the link, it offers a more clear download and the ZIP includes the manual. Regarding the rule you suggest it would not work in this case, because in addition to change the directory path he needs to capture the number at the end of the original URL and put it at the end of the rewritten URL, and in you rule you are capturing the text before the subdirs. Commented Sep 22, 2017 at 9:46
  • About the link, not a problem. =) Regarding the rewrite rule, I appreciate the clarification. I am terrible at that stuff. ;-) Commented Sep 22, 2017 at 9:52
  • 1
    Thanks to both Alberto Martinez and @Anaksunaman. I'm reviewing the solution with my IT team for feasibility but looks good so far!
    – Kevin O.
    Commented Sep 22, 2017 at 11:55
0

As I understand it, HOSTS is IP based only. Is there a way for me to force anything calling the first example to return the second example?

The simple solution is to set up a web server or other proxy to transparently rewrite URL requests for the old setup to the new one. This can be accomplished with Apache and mod_rewrite (as nicely detailed in @alberto-martinez's answer).

I'm not the only one who uses this tool and would like to deploy a solution if possible to other members of my team who, unfortunately, are less technically skilled than I am. Ideally we'd like a set-and-forget solution[.]

Since you also expressed a desire to avoid hosts files (though perhaps for more technical reasons), you may want to consider using a DNS solution in conjunction with Apache.

Basically, it would be an additional local redirect (via DNS) of subdomain1.domain.com to the server housing Apache (or whatever). So long as the people using the software were connected to that DNS server, then there would be no need to modify the hosts file of each computer.

Caveats

  • You would need to be specific about using subdomain1.domain.com. If you just put an entry for domain.com, this would cause issues (as subdomain2.domain.com would be caught as well).

  • If you did use this solution, you might have to tweak the rewrite rule (I ran into issues, but per Alberto's kind comments, this seems based on testing methodology/user error).

  • There are potential security implications with cloning public Top Level Domains (such as .com), and possibly more with an active domain, so you may want to do some research before deploying a similar solution.

You must log in to answer this question.

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