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
hostfrom the HTTP Header. - Gets the
path segmentsfrom the HTTP Header as theaccess key. - Enters the Global Management page when
- The
access keyequals toGlobal Management Key. And, - The
hostis allowed to enter Global Management page when- The
hostexists inGlobal Management Enabled Hosts. Or, - The list
Global Management Enabled Hostsis empty.
- The
- The
- Resolves by aliases when the
hostequals(*1) theAliascolumn of one record inAliasesfrom Global Management, choosingTargetas the new value ofhostand restart this step. Aliases could be resolved recursively with 16 as the max depth. - Processes the request against the domain when the
hostequals(*1)Domaincolumn of one record inDomainsfrom Global Management.- Enters the Domain Management page when
access keyequals toDomain Management Key. - Resolves by redirects when the
access keyequals(*2) the address column of one record in Redirects from Domain Management of this domain.- When the
Targetfrom the record matched is starting with>, gets the text after>fromTargetas the new value ofaccess keyand restart this step. Redirects could be resolved recursively with 16 as the max depth. Or, - Redirects to
Targetspecified of the record matched.
- When the
- Redirects to
Default Redirect Targetspecified in Domain Management of this domain if it is not empty.
- Enters the Domain Management page when
- Redirects to
Default Redirect Targetspecified 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.comwill be matched with the hostexample.comandexample.com:443. - The record
example.com:8080will be matched with the hostexample.com:8080only.
- The record
- The record with the key ends with
:443in 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 307orUse HTTP 308is selected. Or HTTP 307 will be used. - When
Attach Query Processis enabled,- If the query string exists in the request:
- When character
?presents in the target of the redirection record, the query string from the request will be appended to the target, using&as the connector. - When character
?absents from the target of the redirection, the query string from the request will be appended to the target, using?as the connector.
- When character
- If the query string absents from the request: The target of the redirection record will be used without modification.
- Note: This function valids for redirection only. The value will be ignored for the record with target starting with
>,<and".
- If the query string exists in the request:
- When
Attach Query Processis disabled, the query string, if exists, from the request will be dropped. The target of the redirection record will be used without modification.