Skip to content

fix: multilingual URL rendering with uglyURLs#14689

Open
xingzihai wants to merge 1 commit intogohugoio:masterfrom
xingzihai:fix/issue-14576-multilingual-uglyurl
Open

fix: multilingual URL rendering with uglyURLs#14689
xingzihai wants to merge 1 commit intogohugoio:masterfrom
xingzihai:fix/issue-14576-multilingual-uglyurl

Conversation

@xingzihai
Copy link
Copy Markdown
Contributor

This PR fixes multilingual URL rendering when uglyURLs is enabled.\n\n## Changes\n- Fixed URL rendering to properly handle multilingual sites with uglyURLs\n\nFixes #14576

Fixes gohugoio#14576

When using multilingual website with defaultContentLanguageInSubdir=true
and uglyURLs=true, Hugo was incorrectly creating a redirect at the
default language home page that would overwrite the actual content.

The issue was in renderDefaultSiteRedirect where the code was creating
redirects for all directory levels, including the directory that
contains the index file. With uglyURLs enabled, the homeLink ends with
an index file (e.g., '/en/index.html'), and the redirect path would
resolve to the same file, overwriting the home page.

This fix adds a check to skip creating redirects for the directory
that contains the index file when uglyURLs is enabled.

Changes:
- hugolib/site_render.go: Add skipCount logic to skip redirect creation
  for directories containing index files
- hugolib/sitesmatrix/sitematrix_integration_test.go: Add test case
  TestSitesMatrixUglyURLsDefaultContentLanguageInSubdir to verify the fix

Tested with:
- Manual test site with multilingual (en, zh) + defaultContentLanguageInSubdir=true + uglyURLs=true
- All home pages correctly generated with actual content
- Root redirect correctly points to default language home page
@gemini-code-assist
Copy link
Copy Markdown

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@jmooring
Copy link
Copy Markdown
Member

This PR does not address this comment:
#14576 (comment)

If we decide on a partial fix to close #14576, we must create a new issue to cover the multihost scenario.

I also think that we may want to address #13346 concurrently.

@Qodo-Free-For-OSS
Copy link
Copy Markdown

Hi, renderDefaultSiteRedirect skips creating a redirect for the directory containing the home output whenever the homeLink filename starts with of.BaseName + ".". Alias publishing always writes redirects to index.html for directory aliases, so this skip can incorrectly suppress redirects when the home page is written to a different filename (e.g., index.b via custom HTML suffix), where no overwrite/collision would occur.

Severity: remediation recommended | Category: correctness

How to fix: Skip only on real collision

Agent prompt to fix - you can give this to your LLM of choice:

Issue description

renderDefaultSiteRedirect currently decides whether to skip generating the alias for the directory containing the home output by checking if the last path segment of homeLink looks like an index file (HasPrefix(last, of.BaseName + ".")). This is only a proxy for the real problem (alias target overwriting the home page) and can be wrong when the home page’s output filename is not index.html.

Issue Context

  • Alias publishing resolves directory aliases to .../index.html via targetPathAlias, independent of output format suffix/baseName.
  • Home output filenames can vary with custom media type suffixes / output format configuration.

Fix Focus Areas

  • hugolib/site_render.go[369-383]
  • hugolib/alias.go[123-187]

Suggested approach

Instead of checking the link string, compute whether the would-be alias publish path would resolve to the same target file as the home page:

  1. Compute the home page target filename (e.g., home.pageOutput.targetPaths().TargetFilename).
  2. Compute the alias target filename for the directory alias you are about to add (use the same logic as publishing, e.g., call/replicate targetPathAlias on the alias path).
  3. Only skip the last directory alias when aliasTargetFilename == homeTargetFilename.
    This keeps the current fix for /en/index.html overwrites while not disabling redirects unnecessarily for custom suffix/baseName setups.

Found by Qodo code review. FYI, Qodo is free for open-source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants