Skip to content

Commit 2e0a1ae

Browse files
committed
Publish
0 parents  commit 2e0a1ae

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# URL Redirection Service
2+
3+
This is a simple web page that redirects to the URL encoded in the URI fragment (the part after the "#").
4+
For example <https://tom93.github.io/redirect/#https://example.com> redirects to <https://example.com>.
5+
It is useful for preventing "intelligent" software from processing URLs of a particular form.
6+
7+
The target URL can be encoded using `encodeURIComponent()`.
8+
Alternatively, the following function preserves most special characters for readability while still producing a valid encoding:
9+
10+
```js
11+
function encodeURIFragment(s) {
12+
/* Encode a string for inclusion in the URL fragment. */
13+
/*
14+
Relevant syntax specification (https://www.rfc-editor.org/rfc/rfc3986.html):
15+
fragment = *( pchar / "/" / "?" )
16+
pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
17+
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
18+
sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
19+
/ "*" / "+" / "," / ";" / "="
20+
21+
The set of safe characters is almost the same as for encodeURI(), except encodeURI() does not encode "#".
22+
So we just use encodeURI() and then encode "#" manually.
23+
*/
24+
return encodeURI(s).replace(/#/g, "%23");
25+
}
26+
```

docs/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width,initial-scale=1">
6+
<link rel="icon" href="data:,">
7+
<title>Redirecting...</title>
8+
<style>body { font-family: sans-serif; }</style>
9+
</head>
10+
<body>
11+
<div id="message"></div>
12+
<script>
13+
var url = decodeURIComponent(location.hash.substring(1));
14+
if (!url) {
15+
document.getElementById("message").textContent = "Error: No URL specified";
16+
} else if (!/^http[s]?:\/\//.test(url)) {
17+
document.getElementById("message").textContent = "Error: Invalid URL";
18+
} else {
19+
location.href = url;
20+
}
21+
</script>
22+
</body>
23+
</html>

0 commit comments

Comments
 (0)