更改浏览器URL栏文本

时间:2020-04-22 19:56:49

标签: url redirect cloudflare cloudflare-workers

我在托管服务提供商中拥有一个域(仅是域)。该域指向另一个网址:

domain.com-->anotherdomain.dom/path

另一方面,我将域添加到了Cloudflare帐户中,如下所示:

domain.com-->Cloudflare-->anotherdomain.dom/path

问题在于,输入domain.dom后,浏览器URL栏中的URL文本为anotherdomain.dom/path,我需要将其设为domain.com

浏览器网址栏中是否可以有domain.com?我需要在.htaccess文件中编写一些代码还是在anotherdomain.com中编写一些代码吗?我是否必须在Cloudflare内部做某事(也许与“工人”一起做)?

1 个答案:

答案 0 :(得分:2)

听起来好像当前您的域domain.com已设置为重定向。当用户在其浏览器中访问domain.com时,服务器(Cloudflare)会显示一条消息:“请转到anotherdomain.com/path。”然后,浏览器的行为就像用户实际在地址栏中键入anotherdomain.com/path一样。

听起来,您想让domain.com成为代理。当收到domain.com的请求时,您希望Cloudflare从anotherdomain.com/path获取内容,然后返回该内容以响应原始请求。

为此,您将需要使用Workers。 Cloudflare Workers允许您编写任意JavaScript代码来告诉Cloudflare如何处理域的HTTP请求。

以下是一个工作脚本,可实现您想要的代理行为:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  // Parse the original request URL.
  let url = new URL(request.url);

  // Change domain name.
  url.host = "anotherdomain.org";
  // Add path prefix.
  url.pathname = "/path" + url.pathname;

  // Create a new request with the new URL, but
  // copying all other properties from the
  // original request.
  request = new Request(url, request);

  // Send the new request.
  let response = await fetch(request);

  // Use the response to fulfill the original
  // request.
  return response;
}
相关问题