0

Suppose i have a link generated in php:

http://localhost/Example/www.someDomain.com

now i would like to go to www.someDomain.com , can you please help me with the proper method (probably via .htaccess) to redirect to desired location.

I tried using php header function and js window.location.replace(" ") but it dint' helped.

***Problem with window.location.replace("www.someDomain.com") is that instead of redirectng to www.someDomain.com , the page redirects to http://localhost/www.someDomain.com


I am using Wamp server if it helps. Help please and also suggest some nice resource to study about .htaccess Thanks

Paras Rautela
  • 56
  • 1
  • 7
  • `http://` before the `www` with the window.location.replace. – Ian Jul 18 '14 at 17:51
  • If the url contains http:// or get parameters, I believe it would get rather sticky. If you don't mind using Javascript, you can use a hash: `http://localhost/Example#www.someDomain.com` then in Javascript you'd simply get it with `var url = window.location.hash.replace('#', '');` then `window.location.replace('http://'+url);` – iautomation Jul 18 '14 at 17:53
  • possible duplicate of [How can I make a redirect page in jQuery/JavaScript?](http://stackoverflow.com/questions/503093/how-can-i-make-a-redirect-page-in-jquery-javascript) – Ian Jul 18 '14 at 17:56

3 Answers3

1

If I understood.

you need to put te entire URL with "http://".

Javascript:

<script>
window.location.href=("URL");
</script>

OR in php:

<?php
header("location:$redirect");
?>
ivanfromer
  • 329
  • 1
  • 5
0

You can use window.location or PHP header() function, but for redirect to another site must prefix with http://.

header("Location: http://www.example.com");

You can also do the same through Apache .htaccess with a Redirect rule.

paulodiovani
  • 1,208
  • 2
  • 16
  • 34
0

htaccess is NOT going to help you at all. If all your links look like: http://localhost/... then in order for htaccess to redirect, everyone who goes to your site would have to be running a webserver on their local machine and have an htaccess file that redirects to your site.

You need to fix your php scripts so they generate the correct hostname. Using javascript to redirect isn't even a viable solution, because in order for the location to be "localhost", they're already loading no content because they're trying to connect to their own machine.

If for whatever crazy reason you can't fix your scripts, you may have to look into using mod_proxy_html on your site and dynamically change all your "localhost" to your domain.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • this is for a personal project running on my local machine. I just am curious about possibilities of how htaccess can be used for url rewrite. What i want is to extract parts of a url to produce a new url... for example a request to 'www.example.com/a/b' should be redirected to 'www.b.com' – Paras Rautela Jul 23 '14 at 17:41