2

I've got an Indy HTTP server using the TIdHTTPServer component. I'm wanting the browser URL to be rewritten like Apache and IIS do using URL Rewrite modules.

For example: If someone goes to https://www.mydomain2.com I want the URL in the browser to show https://www.mydomain1.com.

I'm pretty sure using Javascript's history.pushState is not the answer.

I have tried using Javascript's history.pushState('data to be passed', 'Title of the page', '/www.mydomain1.com'); as an alternative. However, when I go to https://www.mydomain2.com it appends to the current URL like https://www.mydomain2.com/www.domain1.com.

My understanding is history.pushState doesn't allow for a full URL replacement due to security issues.

My server headers being (and I'm just shooting in the dark):

aResponseInfo.CustomHeaders.AddValue('Access-Control-Allow-Origin','*');
aResponseInfo.CustomHeaders.AddValue('Access-Control-Allow-Methods','*');
aResponseInfo.CustomHeaders.AddValue('Access-Control-Allow-Headers',
  'Origin, X-Requested-With, Content-Type, Accept, Authorization');
aResponseInfo.CustomHeaders.AddValue('Cache-Control', 'no-cache');

I'm having trouble finding a solution.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
aknapple
  • 51
  • 2

1 Answers1

4

The only way to do this from the server side is to instruct the client to request a new URL. You can use the TIdHTTPResponseInfo.Redirect() method for that purpose, eg:

if ARequestInfo.Host = 'www.mydomain2.com' then
  AResponseInfo.Redirect('https://www.mydomain1.com');

But, note that the client will no longer BE AT https://www.mydomain2.com anymore, it will navigate itself to https://www.mydomain1.com instead.

If you want the client to STAY AT https://www.mydomain2.com but DISPLAY https://www.mydomain1.com, that can only be done via client side scripting that manipulates the browser's address bar.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • My understanding is that there is not a way of doing client side scripting due to security reasons? Would you know where to point me to find out how this can be done? – aknapple Dec 20 '18 at 04:28
  • See [How to modify the URL without reloading the page?](https://stackoverflow.com/questions/824349/) – Remy Lebeau Dec 20 '18 at 04:33
  • Remy, while I try to figure out a solution there seems to be valid reason for wanting to do URL Rewrites. is there a chance this might be available in a future Indy build? – aknapple Dec 20 '18 at 04:50
  • See [How does url rewrite works?](https://stackoverflow.com/questions/6430858/). "Redirect" is already implemented in Indy. "Rewrite" is not, but there is nothing stopping you from implementing "Rewrite" yourself in your `OnCommand...` handlers today, it is just a matter of how you decide to interpret the requested URLs. And no, there are no plans to have Indy implement "Rewrite" for you anytime soon. – Remy Lebeau Dec 20 '18 at 07:55