Hey,
I have a website example.com hosted on a server and now I launched a new website example.com/abc that is hosted on another server. For this to work I using a worker with reverse proxy and a dns record with the subdomain abc.example.com.
Everything is working fine except for two issue that I have now:
1. Implementing redirects on example.com/abc, for example I want to redirect example.com/abc/author to example.com/abc but I'm not able to do it. (I tried using htaccess "Redirect 301 /author https://example.com/abc).
2. Using url variables, for example I want to access example.com/search?s=asb and I'm not able to access the variable and the value. (I tried using $_GET in PHP).
Here is the worker code and I also have a dns record (A, abc, server IP, with DNS only):
// keep track of all our blog endpoints here
const myBlog = {
hostname: "abc.example.com",
targetSubdirectory: "/abc",
assetsPathnames: ["/public/", "/assets/"]
}
async function handleRequest(request) {
// returns an empty string or a path if one exists
const formatPath = (url) => {
const pruned = url.pathname.split("/").filter(part => part)
return pruned && pruned.length > 1 ? `${pruned.join("/")}` : ""
}
const parsedUrl = new URL(request.url)
const requestMatches = match => new RegExp(match).test(parsedUrl.pathname)
// if its blog html, get it
if (requestMatches(myBlog.targetSubdirectory)) {
console.log("this is a request for a blog document", parsedUrl.pathname)
const targetPath = formatPath(parsedUrl).substr(myBlog.targetSubdirectory.length)
return fetch(`https://${myBlog.hostname}/${targetPath}`)
}
// if its blog assets, get them
if ([myBlog.assetsPathnames].some(requestMatches)) {
console.log("this is a request for blog assets", parsedUrl.pathname)
const assetUrl = request.url.replace(parsedUrl.hostname, myBlog.hostname);
return fetch(assetUrl)
}
console.log("this is a request to my root domain", parsedUrl.host, parsedUrl.pathname);
// if its not a request blog related stuff, do nothing
return fetch(request)
}
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
Can someone help with this?
Thanks