Short url redirector for Cloudflare Worker with D1 database.
Language: JavaScript
Platform: Cloudflare Worker with D1 database.
- Deploy in Cloudflare directly using Worker.
- Multiple host names supported.
See deployment file.
See management file.
When a request is received by ShortUrl, it will follow these steps.
- Gets the
host
from the HTTP Header. - Gets the
path segments
from the HTTP Header as theaccess key
. - Enters the Global Management page when
- The
access key
equals toGlobal Management Key
. And, - The
host
is allowed to enter Global Management page when- The
host
exists inGlobal Management Enabled Hosts
. Or, - The list
Global Management Enabled Hosts
is empty.
- The
- The
- Resolves by aliases when the
host
equals(*1) theAlias
column of one record inAliases
from Global Management, choosingTarget
as the new value ofhost
and restart this step. Aliases could be resolved recursively with 16 as the max depth. - Processes the request against the domain when the
host
equals(*1)Domain
column of one record inDomains
from Global Management.- Enters the Domain Management page when
access key
equals toDomain Management Key
. - Resolves by redirects when the
access key
equals(*2) the address column of one record in Redirects from Domain Management of this domain.- When the
Target
from the record matched is starting with>
, gets the text after>
fromTarget
as the new value ofaccess key
and restart this step. Redirects could be resolved recursively with 16 as the max depth. Or, - Redirects to
Target
specified of the record matched.
- When the
- Redirects to
Default Redirect Target
specified in Domain Management of this domain if it is not empty.
- Enters the Domain Management page when
- Redirects to
Default Redirect Target
specified in Global Management.
- Host name matching is case insensitive.
- Port number 443 should not present, but all others should and will be treated separately. For example:
- The record
example.com
will be matched with the hostexample.com
andexample.com:443
. - The record
example.com:8080
will be matched with the hostexample.com:8080
only.
- The record
- The record with the key ends with
:443
in domains or aliases will not be matched unless it's pointed by other matched alias records.
Name matching could be case sensitive or insensitive, based on the setting Ignore Case When Matching
specified in the Domain Management of the related domain.
When redirecting:
- HTTP 308 will be used, when
Use HTTP 308 instead of 307
orUse HTTP 308
is selected. Or HTTP 307 will be used. - When
Attach Query Process
is enabled and the query string exists from the request:- When character
?
presents in the target of the redirection,&
and the query string from the request will be appended. - When character
?
absents from the target of the redirection,?
and the query string from the request will be appended.
- When character
- When
Attach Query Process
is disabled, the query string, if exists, from the request will be dropped and will not be passed into the redirection target.