Understanding the replaceTwitter Preference in Nitter: Custom URL Rewriting Explained

The replaceTwitter preference in Nitter lets users specify a custom hostname to rewrite all Twitter-related links—including t.co, twitter.com, and x.com URLs—so they point to a user-controlled Nitter instance instead of external Twitter servers.

The replaceTwitter preference is a privacy-focused configuration option in the zedeus/nitter open-source frontend for Twitter. Located within the Link replacements section of the preferences UI, this setting enables administrators and end-users to intercept and transform Twitter URLs within tweets, ensuring all external Twitter traffic routes through a specified Nitter host. This article examines the implementation details, source code locations, and practical applications of this URL rewriting feature.

What Is the replaceTwitter Preference?

The replaceTwitter preference accepts a custom hostname string in the Nitter preferences interface. When populated, this value instructs Nitter's server-side rendering engine to substitute any Twitter domain—such as twitter.com, x.com, or t.co short-links—with the provided hostname before displaying content to the viewer.

When a non-empty value is supplied, the server’s URL-rewriting routine swaps the original Twitter domain with the given hostname. This ensures every Twitter link presented to the viewer points to the user’s own Nitter instance or any other frontend they control.

Implementation Details

The URL transformation operates through a two-stage process involving preference storage and runtime text processing in the Nim codebase.

Preference Definition in src/prefs_impl.nim

The preference is declared in src/prefs_impl.nim at lines 118-121 as a text input field. This definition integrates the setting into the preferences UI and provides a placeholder prompting for the Nitter hostname.


# Preference entry (src/prefs_impl.nim)

replaceTwitter(input, ""):
  "Twitter -> Nitter"
  placeholder: "Nitter hostname"

URL Replacement Logic in src/formatters.nim

The actual rewriting occurs in src/formatters.nim (lines 58-71), where the replaceUrls routine checks if prefs.replaceTwitter contains a non-empty value. If configured, the system strips trailing slashes using strip(prefs.replaceTwitter, chars={'/'}) and applies regex-based substitutions to rewrite t.co short links, plain Twitter URLs, and card links.


# URL rewriting logic (src/formatters.nim)

if prefs.replaceTwitter.len > 0:
  let twitterHost = strip(prefs.replaceTwitter, chars={'/'})
  # Rewrite t.co short links

  result = result.replace(tco, https & twitterHost & "/t.co")
  # Rewrite plain twitter.com/x.com links

  result = result.replace(xRegex, twitterHost)
  result = result.replacef(xLinkRegex, a(
    # … additional link formatting logic

  ))

The src/routes/preferences.nim file serves the preferences page where users can edit the replaceTwitter value through the web interface.

Code Example: Configuring URL Rewriting

When a user sets replaceTwitter to nitter.example.com, the server transforms links as follows:


# User configuration input

replaceTwitter = "nitter.example.com"

# Resulting transformation

# Original: https://twitter.com/username/status/123

# Rewritten: https://nitter.example.com/username/status/123

The system processes the prefs.replaceTwitter string at render time, ensuring that any embedded Twitter links within tweet text, card URLs, or metadata redirect to the specified instance.

Use Cases for Custom URL Rewriting

  • Privacy-Preserving Redirects: All Twitter traffic stays within the Nitter instance, preventing external tracking by Twitter/X servers.
  • Custom Domain Hosting: Administrators hosting a personal Nitter instance under a custom domain (e.g., nitter.myself.org) can ensure all internal links remain within their domain.
  • Selective Link Rewriting: Users can disable link transformation entirely by leaving the replaceTwitter field blank, allowing standard Twitter URLs to pass through unchanged.

Summary

  • The replaceTwitter preference in src/prefs_impl.nim defines a user-configurable hostname for URL substitution.
  • The replaceUrls logic in src/formatters.nim performs runtime rewriting of t.co, twitter.com, and x.com links when prefs.replaceTwitter is non-empty.
  • Configuration requires only the hostname string (e.g., nitter.example.com) without trailing slashes.
  • The feature supports privacy-focused browsing and self-hosted Nitter instances by ensuring all Twitter links route through user-controlled infrastructure.
  • Link rewriting can be disabled by leaving the preference field blank.

Frequently Asked Questions

What does the replaceTwitter preference do in Nitter?

The replaceTwitter preference specifies a custom hostname that Nitter uses to rewrite Twitter-related links found in tweets. When configured, the system replaces domains like twitter.com, x.com, and t.co with the provided hostname, ensuring all Twitter traffic routes through the specified Nitter instance instead of external servers.

Where is the replaceTwitter preference defined in the source code?

The preference is defined in src/prefs_impl.nim at lines 118-121 as an input field within the Link replacements section. The actual URL transformation logic resides in src/formatters.nim (lines 58-71), where the system checks prefs.replaceTwitter and applies regex-based substitutions to rewrite links before rendering the page.

Can I use replaceTwitter with any domain or only Nitter instances?

You can configure replaceTwitter with any valid hostname, though it is designed primarily for Nitter or compatible frontends. The system will prepend https:// to the hostname and append the original path, so the target domain must support the same URL structure as Nitter to function correctly.

To disable URL rewriting, leave the replaceTwitter field blank in the preferences UI. When prefs.replaceTwitter.len equals zero, the conditional logic in src/formatters.nim skips the replacement routine, allowing all Twitter links to remain unchanged and point directly to the original Twitter/X domains.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →