-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirects
53 lines (49 loc) · 1.91 KB
/
redirects
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Example using Service Worker to Implement Redirects
* You can see this here https://cloudflareworkers.com/#78a5be935eeb2042b70497a712e46fd2:https://www.cloudflare.com/example
*/
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request))
})
/**
* Redirect if matches case
* @param {Request} request
*/
async function fetchAndApply(request) {
// This extracts the path so you have a pattern to match
var url = new URL(request.url);
var path = url.pathname;
// console.log('Path', path)
// This is to set-up any default protocol or host to minimize multiple redirects
// You can switch to using requested protocol (2nd option) if needed for dynamic environments
var site = "https://www.cloudflare.com";
// var site = url.protocol + "//" + url.host;
// console.log('Site', site)
switch(path) {
// Cases match the path you are redirecting.
case "/example":
// Update url here to point to redirection destination
return Response.redirect(site + '/products/cloudflare-workers/', 301);
break;
case "/example-2":
// Alternatively, you can set the full response if pointing to external url.
return Response.redirect('https://support.cloudflare.com/', 301);
break;
case "/example-3":
return Response.redirect('https://developers.cloudflare.com/workers/api/route-matching/', 301);
break;
case "/test":
return Response.redirect(site + '/plans', 301);
break;
case "/test-2":
return Response.redirect('https://developers.cloudflare.com/workers/about/how-workers-work/', 301);
break;
case "/test-3":
return Response.redirect('https://www.hivedigital.com/2018/12/18', 301);
break;
default:
const response = await fetch(request)
return response
break;
}
}