Leokratis
05/13/2023, 6:38 PMwebsite2.com
under the`website.com/2` domain and so far I have managed to do that using the code:
js
export default {
async fetch(request) {
async function MethodNotAllowed(request) {
return new Response(`Method ${request.method} not allowed.`, {
status: 405,
headers: {
Allow: "GET",
},
});
}
// Only GET requests work with this proxy.
if (request.method !== "GET") return MethodNotAllowed(request);
return fetch(`https://example.com`);
},
};
It works fine but the assets are not loading and I get the plain html version of the website.
I have setup the following worker routes:
- *dreamcafe.eu.org/assets/images*
- *dreamcafe.eu.org/assets*
- https://dreamcafe.eu.org/guides/
with the *dreamcafe.eu.org/assets*
route it should make the website appear as normal but it doesn't does anyone know why?
https://cdn.discordapp.com/attachments/1107014041911689277/1107014208769499166/image.png▾
Leokratis
05/13/2023, 6:40 PMhttps://cdn.discordapp.com/attachments/1107014041911689277/1107014475585958069/image.png▾
Walshy | Pages
05/13/2023, 6:42 PMWalshy | Pages
05/13/2023, 6:42 PMLeokratis
05/13/2023, 6:44 PMWalshy | Pages
05/13/2023, 6:45 PMLeokratis
05/13/2023, 6:45 PMLeokratis
05/13/2023, 6:48 PMLeokratis
05/13/2023, 6:49 PMPage Not Found
Looks like you've followed a broken link or entered a URL that doesn't exist on this site.
Leokratis
05/13/2023, 6:52 PMjs
export default {
async fetch(request) {
async function MethodNotAllowed(request) {
return new Response(`Method ${request.method} not allowed.`, {
status: 405,
headers: {
Allow: "GET",
},
});
}
// Only GET requests work with this proxy.
if (request.method !== "GET") return MethodNotAllowed(request);
// Get the requested URL from the request object
const url = new URL(request.url);
// Extract the path from the URL and append it to the target URL
const targetUrl = `https://example.com${url.pathname}`;
// Fetch the target URL
return fetch(targetUrl);
},
};
I think I just don't fetch the /subdomain correctly nowLeokratis
05/13/2023, 6:53 PMLeokratis
05/13/2023, 6:53 PMChaika
05/13/2023, 7:06 PMdreamcafe.eu.org/guides/
which is trying to fetch example.com
with the same path, excluding /guides?Leokratis
05/13/2023, 7:08 PMLeokratis
05/13/2023, 7:08 PMChaika
05/13/2023, 7:09 PMLeokratis
05/13/2023, 7:10 PMLeokratis
05/13/2023, 7:10 PMLeokratis
05/13/2023, 7:10 PMLeokratis
05/13/2023, 7:10 PMLeokratis
05/13/2023, 7:10 PMhttps://cdn.discordapp.com/attachments/1107014041911689277/1107022160234565712/image.png▾
Chaika
05/13/2023, 7:11 PMChaika
05/13/2023, 7:12 PMLeokratis
05/13/2023, 7:14 PMexample.com/subdomain/path
Leokratis
05/13/2023, 7:16 PMjs
export default {
async fetch(request) {
async function MethodNotAllowed(request) {
return new Response(`Method ${request.method} not allowed.`, {
status: 405,
headers: {
Allow: "GET",
},
});
}
// Only GET requests work with this proxy.
if (request.method !== "GET") return MethodNotAllowed(request);
// Get the requested URL from the request object
const url = new URL(request.url);
// Extract the path from the URL and append it to the target URL
const targetUrl = `https://sub.example.com${url.pathname}${url.search}`;
// Fetch the target URL
return fetch(targetUrl);
},
};
and in the preview it looks fine but I get a 404 error when I try to access it through the domainLeokratis
05/13/2023, 7:16 PMhttps://cdn.discordapp.com/attachments/1107014041911689277/1107023694443851796/image.png▾
Chaika
05/13/2023, 7:17 PMhttps://dreamcafe.eu.org/cookies.txt
to fetch something like sub.dreamcafe.eu.org/cookies.txt
Chaika
05/13/2023, 7:18 PMLeokratis
05/13/2023, 7:18 PMLeokratis
05/13/2023, 7:19 PMLeokratis
05/13/2023, 7:20 PMjs
export default {
async fetch(request) {
async function MethodNotAllowed(request) {
return new Response(`Method ${request.method} not allowed.`, {
status: 405,
headers: {
Allow: "GET",
},
});
}
// Only GET requests work with this proxy.
if (request.method !== "GET") return MethodNotAllowed(request);
// Get the requested URL from the request object
const url = new URL(request.url);
// Extract the path from the URL and append it to the target URL
const targetUrl = `https://guides.dreamcafe.eu.org${url.pathname}${url.search}`;
// Fetch the target URL
return fetch(targetUrl);
},
};
Chaika
05/13/2023, 7:26 PMChaika
05/13/2023, 7:35 PMLeokratis
05/13/2023, 7:35 PMLeokratis
05/13/2023, 7:35 PMLeokratis
05/13/2023, 7:35 PMLeokratis
05/13/2023, 7:35 PMLeokratis
05/13/2023, 7:36 PMhttps://cdn.discordapp.com/attachments/1107014041911689277/1107028511543283753/image.png▾
Leokratis
05/13/2023, 7:36 PMChaika
05/13/2023, 7:36 PMhttps://cdn.discordapp.com/attachments/1107014041911689277/1107028589972574258/image.png▾
Chaika
05/13/2023, 7:36 PMLeokratis
05/13/2023, 7:36 PMLeokratis
05/13/2023, 7:36 PMLeokratis
05/13/2023, 7:36 PMChaika
05/13/2023, 7:36 PMLeokratis
05/13/2023, 7:36 PMChaika
05/13/2023, 7:36 PMLeokratis
05/13/2023, 7:37 PMLeokratis
05/13/2023, 7:37 PMLeokratis
05/13/2023, 7:37 PMLeokratis
05/13/2023, 7:37 PMLeokratis
05/13/2023, 7:37 PMChaika
05/13/2023, 7:38 PMChaika
05/13/2023, 7:38 PMChaika
05/13/2023, 7:38 PM*dreamcafe.eu.org/assets/images*
*dreamcafe.eu.org/assets*
Leokratis
05/13/2023, 7:38 PMLeokratis
05/13/2023, 7:38 PMLeokratis
05/13/2023, 7:39 PMChaika
05/13/2023, 7:39 PMChaika
05/13/2023, 7:40 PMChaika
05/13/2023, 7:41 PMjavascript
const REAL_URL = "guides.dreamcafe.eu.org";
const FAKE_SUBPATH = "/guides";
export default {
async fetch(request, env, ctx) {
var url = new URL(request.url);
url.hostname = REAL_URL;
url.pathname = url.pathname.replace(FAKE_SUBPATH, "");
var res = await fetch(url, request);
const contentType = res.headers.get("Content-Type");
// If the response is HTML, it can be transformed with
// HTMLRewriter -- otherwise, it should pass through
if (contentType.startsWith("text/html")) {
return rewriter.transform(res);
} else {
return res;
}
},
};
// Used to rewrite link tags, img tags, etc
class AttributeRewriter {
constructor(attributeName) {
this.attributeName = attributeName;
}
element(element) {
const attribute = element.getAttribute(this.attributeName);
if (attribute) {
element.setAttribute(this.attributeName, FAKE_SUBPATH + attribute);
}
}
}
// Rewriter
const rewriter = new HTMLRewriter()
.on("a", new AttributeRewriter("href"))
.on("base", new AttributeRewriter("href"))
.on("link", new AttributeRewriter("href"))
.on("img", new AttributeRewriter("src"));
Chaika
05/13/2023, 7:41 PMdreamcafe.eu.org/guides*
Leokratis
05/13/2023, 7:41 PMChaika
05/13/2023, 7:42 PMLeokratis
05/13/2023, 7:43 PMhttps://cdn.discordapp.com/attachments/1107014041911689277/1107030350074499173/image.png▾
Chaika
05/13/2023, 7:44 PM/guides
with a route there. If you want your page to be under /guides/
, change the FAKE_SUBPATH variable to /guides/
Leokratis
05/13/2023, 7:45 PMLeokratis
05/13/2023, 7:45 PMLeokratis
05/13/2023, 7:45 PMLeokratis
05/13/2023, 7:46 PMLeokratis
05/13/2023, 7:46 PMLeokratis
05/13/2023, 7:46 PM*dreamcafe.eu.org/assets/images*
*dreamcafe.eu.org/assets*
Leokratis
05/13/2023, 7:46 PMChaika
05/13/2023, 7:47 PMjs
const FAKE_SUBPATH = "/guides";
to
js
const FAKE_SUBPATH = "/guides/";
Chaika
05/13/2023, 7:47 PMdreamcafe.eu.org/guides/*
Leokratis
05/13/2023, 7:49 PMhttps://cdn.discordapp.com/attachments/1107014041911689277/1107031960662056970/image.png▾
Leokratis
05/13/2023, 7:49 PMLeokratis
05/13/2023, 7:49 PMChaika
05/13/2023, 7:50 PMhttps://cdn.discordapp.com/attachments/1107014041911689277/1107032087699140628/image.png▾
Leokratis
05/13/2023, 7:50 PMChaika
05/13/2023, 7:51 PMChaika
05/13/2023, 7:51 PMLeokratis
05/13/2023, 7:52 PMhttps://cdn.discordapp.com/attachments/1107014041911689277/1107032646967623680/image.png▾
Leokratis
05/13/2023, 7:52 PMhttps://cdn.discordapp.com/attachments/1107014041911689277/1107032693264367677/image.png▾
Chaika
05/13/2023, 7:52 PMLeokratis
05/13/2023, 7:52 PMChaika
05/13/2023, 7:53 PMLeokratis
05/13/2023, 7:57 PMjs
const REAL_URL = "guides.dreamcafe.eu.org";
const FAKE_SUBPATH = "/guides";
export default {
async fetch(request, env, ctx) {
var url = new URL(request.url);
url.hostname = REAL_URL;
url.pathname = url.pathname.replace(FAKE_SUBPATH, "");
var res = await fetch(url, request);
const contentType = res.headers.get("Content-Type");
if (contentType.startsWith("text/html")) {
return rewriter.transform(res);
} else {
return res;
}
},
};
class AttributeRewriter {
constructor(attributeName) {
this.attributeName = attributeName;
}
element(element) {
const attribute = element.getAttribute(this.attributeName);
if (attribute) {
element.setAttribute(
this.attributeName,
`${FAKE_SUBPATH}/subdomain${attribute}`
);
}
}
}
// Rewriter
const rewriter = new HTMLRewriter()
.on("a", new AttributeRewriter("href"))
.on("base", new AttributeRewriter("href"))
.on("link", new AttributeRewriter("href"))
.on("img", new AttributeRewriter("src"));
Leokratis
05/13/2023, 7:57 PMChaika
05/13/2023, 8:00 PMChaika
05/13/2023, 8:04 PMjs
const REAL_URL = "guides.dreamcafe.eu.org";
const FAKE_SUBPATH = "/guides";
export default {
async fetch(request, env, ctx) {
var url = new URL(request.url);
url.hostname = REAL_URL;
url.pathname = url.pathname.replace(FAKE_SUBPATH, "");
var res = await fetch(url, request);
const newResponse = new Response(res.body, res);
// transform redirects as well
if (newResponse?.headers?.get("Location")) {
newResponse.headers.set("Location", FAKE_SUBPATH + res.headers.get("Location"));
}
const contentType = res.headers.get("Content-Type");
// If the response is HTML, it can be transformed with
// HTMLRewriter -- otherwise, it should pass through
if (contentType.startsWith("text/html")) {
return rewriter.transform(newResponse);
} else {
return newResponse;
}
},
};
// Used to rewrite link tags, img tags, etc
class AttributeRewriter {
constructor(attributeName) {
this.attributeName = attributeName;
}
element(element) {
const attribute = element.getAttribute(this.attributeName);
if (attribute) {
element.setAttribute(this.attributeName, FAKE_SUBPATH + attribute);
}
}
}
// Rewriter
const rewriter = new HTMLRewriter()
.on("a", new AttributeRewriter("href"))
.on("base", new AttributeRewriter("href"))
.on("link", new AttributeRewriter("href"))
.on("img", new AttributeRewriter("src"));
Leokratis
05/13/2023, 8:05 PMLeokratis
05/13/2023, 8:06 PMLeokratis
05/13/2023, 8:06 PMLeokratis
05/13/2023, 8:06 PMChaika
05/13/2023, 8:07 PMshrirambalaji
05/28/2023, 5:52 AMexport default {
async fetch(request: {
url: string | URL; headers: { get: (arg0: string) => string }
}, env: any, ctx: any) {
try {
const subdomain = request.headers.get('host').split('.')[0];
const id = mapSubdomainToId(subdomain);
const destinationURL = new URL(`http://localhost:3001/${id}`);
return fetch(destinationURL);
} catch (error) {
return new Response(null, { status: 404, statusText: "Oops can't find that page" });
}
},
};
For now I'm running this on wrangler locally, so need to proxy to localhost:3001, which is a next.js site.
https://cdn.discordapp.com/attachments/1107014041911689277/1112257012777111573/image.png▾