简体   繁体   English

JavaScript 重定向到基于 URL 的另一个网站

[英]JavaScript Redirect to Page Another website based on URL

Using JavaScript, how can I make it redirect to another site based on the URL?使用 JavaScript,如何使其重定向到基于 URL 的另一个站点? (Example) If someone goes to https://example.com/12345 , it will redirect them to https://example.net/12345 . (示例)如果有人去https://example.com/12345 ,它会将他们重定向到https://example.net/12345 And if someone goes to https://example.com/abc123456 , it will redirect them to https://example.net/abc123456如果有人去https://example.com/abc123456 ,它会将他们重定向到https://example.net/abc123456

How can I do this?我怎样才能做到这一点?

You can use following code for that:您可以为此使用以下代码:

location.href = 'https://example.net/12345'; location.href = 'https://example.net/12345';

location.href = 'https://example.net/abc123456'; location.href = 'https://example.net/abc123456';

Or used following code for that:或为此使用以下代码:

location.replace('https://example.net/12345'); location.replace('https://example.net/12345');

location.replace('https://example.net/abc123456'); location.replace('https://example.net/abc123456');

In the place that you have hosted that domain, See if you can find something that makes it a single page app or a way to rewrite all urls to one page so that it doesn't show 404 not found.在您托管该域的地方,看看您是否可以找到使其成为单页应用程序的东西,或者将所有 url 重写到一个页面的方法,这样它就不会显示 404 not found。 (not certain how you can do that, I only done it with firebase hosting, it has a way of configuring it so that no matter what url you give it, it always shows you the same page, and also the url doesn't get changed ) if you can do that, this is the code you need: (不确定你怎么能做到这一点,我只用 firebase 主机完成它,它有一种配置方式,所以无论你给它什么 url,它总是显示相同的页面,还有 Z572D4E421E5E6B71111A改变)如果你能做到这一点,这是你需要的代码:

let pathname = location.pathname //if the url is https://example.net/1234,  the path name will be /1234

location.href = "https://example.net" + pathname //if you add that to this string, it would be https://example.net/1234

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM