URL Redirection

2019-08-27 06:46发布

I am trying to achieve something like, that is on the below link.

http://www.laterooms.com/

This website is at the above URL. But even if you type .co.uk or .net or .org Like this any Extension. The URL will be automatically redirected to http://www.laterooms.com/

4条回答
啃猪蹄的小仙女
2楼-- · 2019-08-27 07:13

Assuming you own all the domains, and they all point to the same webserver, then this code should do it;

  var href_parts = top.location.href.split('/');
  if (href_parts[2] != 'www.laterooms.com') {
      href_parts[2] = 'www.laterooms.com';
      top.location.href = href_parts.join('/');        
  }

Basically -- have javascript test what domain you currently are on, and if not on the .com then update the URL and store it back into the location.href for the page which will automatically trigger a page-reload with the new URL.

The above code preserve the URL path within the domain, so if somebody types in

   http://www.lateroom.co.uk/mypath/here

will redirect to

  http://www.lateroom.com/mypath/here

If you don't want to preserve the path /mypath/here then the code will be sligthly simpler as you can just hardcode the destination path rather than using the join

查看更多
家丑人穷心不美
3楼-- · 2019-08-27 07:15

If you own/have possession of those other domains..

You could set forwarding (dns settings) for each of the domains you wish to redirect to the primary domain.

That would get you away from having to code a page for each one of those domains.

Much cleaner, and easier.

查看更多
走好不送
4楼-- · 2019-08-27 07:18

You can either use different techniques on server side to force a redirect, or you can send a page containing a redirect Refresh: 0; url=http://www.example.com/

Wikipedia has a good overview on this.

   <head>
   <meta http-equiv="refresh" content="5; URL=http://de.selfhtml.org/">
   </head>
查看更多
贼婆χ
5楼-- · 2019-08-27 07:23

First of all, you should be in possession of the other domains (with the extensions you want). Then you can either use javascript to set top.location.href='http://your.address.com/' or use other means, like setting up your web server to redirect the requests, or your domain name registrar.

查看更多
登录 后发表回答