2

I need to use javasctipt to redirect to a URL

I currently use something like:

<SCRIPT language="JavaScript">
 window.location="http://app1.mydomain.com/red";
</SCRIPT>

But now I need the redirect to be smart and go to a different executable based on the host.

Example

red.mydomain.com -->  http://app1.mydomain.com/red
blue.mydomain.com -->  http://app1.mydomain.com/blue

Further complicating matters I need the parameters to remain in tact.

blue.mydomain.com/?parameter1=abc?paramteter2=123 -->http://app1.mydomain.com/blue/?parameter1=abc?paramteter2=123 

How does one "read" the in URL and do a case statement to branch it out?

How does one keep the paramter string so He can pass it in tact?

JVMX
  • 1,016
  • 2
  • 12
  • 23
  • 1
    What is the question, what have you tried? – RonaldPK Feb 16 '16 at 15:10
  • 1
    Simply parse it with a Regex and then switch the solutions you need. In order to answer at your edit pls see : [http://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript](http://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript) – morels Feb 16 '16 at 15:14
  • 1
    You don't use regex for URLs. http://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript – Popnoodles Feb 16 '16 at 15:19

1 Answers1

1

Create two Sub domains Red and Blue and then You will have

red.example.com blue.example.com

Then In your .htacsess put in

RewriteCond %{HTTP_HOST} ^red\.example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.red\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/red" [R=301,L]

RewriteCond %{HTTP_HOST} ^blue\.example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.blue\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/blue" [R=301,L]

and now when someone now goes to red.mydomain.com they will be redirected to https://example.com/red before the page loads making the redirect faster than loading the page then redirecting them to then load another page.

  • Remember to change the domain name when copying and also if you don't have a ssl change https:// to http:// on the last line of both of them.

I Hope this helps, Lewis

  • I usually do that, but in this case but I dont have access to .htacess – JVMX Feb 18 '16 at 17:02
  • why is that? can you not just go into cpanel and then set up a redirect? –  Feb 18 '16 at 17:26
  • This is on a corporate server. There are a lot of different server configurations in the world. Many many of them don't use "cpannel". And many of them don't even use Apache. Thanks for asking! – JVMX Feb 18 '16 at 17:40
  • can you create the subdomains ? –  Feb 18 '16 at 17:43
  • Without going into the full topography of the network and the corporate security roadblocks......It needs to be done in Javascript – JVMX Feb 18 '16 at 17:43