I want to redirect a certain URL to another URL. For example, I wan to re-direct everyone who comes to URL1 to URL2 and Everyone from URL2 will remain at URL2.

Is this possible?

for JavaScript questions, I think you'll be better of in a webdevelopment forum.

Comments
I guess this was posted in the wrong section...
This is the javascript section.

Add this to the page that URL1 points to:

window.location = url2; //the url you want to redirect to.

Still 1 line.

if (window.location == url1){window.location = url2;}

Something like this is better off done server-side, imho, to save some precious bandwidth.

Is it correct if I place the code like this:

<script type="text/javascript">if (window.location = http://geekpolice.forumotion.com/){window.location = http://geekpolice.net/;}</div>

It doesn't seem to redirect, it simply stays on that page with only half of it loaded.

No that code is not correct.

<script type="text/javascript">if (window.location == 'http://geekpolice.forumotion.com'){window.location = 'http://geekpolice.net/';}</script>

is better.

The above code will only work if url1 ("http://geekpolice.forumotion.com") appears exactly as is in the browser's address bar (at least, as tested on FF). That means no url parameters, or request extensions. Even if the url starts with www, or has an index.php attached to it, this will not work.

A pretty severe problem I would say, so while I think the best way to do this is still server side, here's a more elaborate solution (this isn't foolproof though).

<script type="text/javascript">
if ((window.location.toLowerCase().substr(0, 32) == 'http://geekpolice.forumotion.com') || (window.location.toLowerCase().substr(0, 35) == 'http://www.geekpolice.forumotion.com'))
{
  window.location = 'http://geekpolice.net/';
}
</script>

Hmmm... The first code works the same as the previous code... the second code doesn't work at all.

No that code is not correct.

<script type="text/javascript">if (window.location == 'http://geekpolice.forumotion.com'){window.location = 'http://geekpolice.net/';}</script>

is better.

The above code will only work if url1 ("http://geekpolice.forumotion.com") appears exactly as is in the browser's address bar (at least, as tested on FF). That means no url parameters, or request extensions. Even if the url starts with www, or has an index.php attached to it, this will not work.

A pretty severe problem I would say, so while I think the best way to do this is still server side, here's a more elaborate solution (this isn't foolproof though).

<script type="text/javascript">
if ((window.location.toLowerCase().substr(0, 32) == 'http://geekpolice.forumotion.com') || (window.location.toLowerCase().substr(0, 35) == 'http://www.geekpolice.forumotion.com'))
{
  window.location = 'http://geekpolice.net/';
}
</script>

Apart from 1 error that I have now corrected, this code seems fine.

<script type="text/javascript">
if ((window.location.toLowerCase().substr(0, 32) == 'http://geekpolice.forumotion.com') || (window.location.toLowerCase().substr(0, 36) == 'http://www.geekpolice.forumotion.com'))
{
  window.location = 'http://geekpolice.net/';
}
</script>

Add this to the page that URL1 points to:

window.location = url2; //the url you want to redirect to.

The `location' property of `window' object is itself an object but you are assigning a string to it. Though it *works*, it's incorrect. You need to set the href property of the location object.

window.location.href = 'http://www.google.com/';

> I want to redirect a certain URL to another URL.

A better solution here would be to employ URL Rewriting; look into the mod_rewrite module of Apache Web server. Your solution fails if Javascript is disabled.

@scru
Concerning your rep comment given to `stultuske', consider the possibility of this thread originally being posted in some other forum and then being moved here.

Comments
Didn't know. Usually they have moved: on them. Apologies.

Use a better development environment like Firefox along with Firebug installed to look out for Javascript errors. If there are no errors, paste the entire code as it is here along with the changes incorporated. The description `doesn't work' is as useless as it can get.

This article has been dead for over six months. Start a new discussion instead.