Skip to content

fix: resolve static front page .md URL generation (#5)#8

Merged
ilicfilip merged 3 commits intomainfrom
issue/5-static-front-page-md-url
Feb 18, 2026
Merged

fix: resolve static front page .md URL generation (#5)#8
ilicfilip merged 3 commits intomainfrom
issue/5-static-front-page-md-url

Conversation

@jdevalk
Copy link
Member

@jdevalk jdevalk commented Feb 15, 2026

Closes #5

Summary

Fixes invalid markdown URL generation when a static page is configured as the front page.

Problem

When a static page is set as the front page (Settings → Reading), get_permalink() returns the site root (e.g., https://example.com/). The plugin was appending .md directly, resulting in https://example.com.md, which looks like an invalid TLD.

Solution

  • Detect when a permalink is the site root (front page)
  • Convert to /index.md instead of .md
  • Added special routing logic to handle /index.md requests
  • Added rewrite rule for index.md

Changes

  • AlternateLinkHandler: Added convert_to_markdown_url() method to generate proper markdown URLs
  • RewriteHandler: Added matching convert_to_markdown_url() method for Accept header negotiation
  • RewriteHandler: Added special case handling in parse_markdown_url() for /index.md
  • RewriteHandler: Added dedicated rewrite rule for ^index\.md$

Test Plan

  • Set a static page as front page (Settings → Reading)
  • Verify <link rel="alternate"> in HTML shows https://example.com/index.md
  • Verify Accept: text/markdown on front page redirects to /index.md
  • Verify /index.md serves front page content as markdown
  • Verify /.md returns 404 (does not resolve to front page)
  • Verify regular posts/pages still work correctly with .md URLs

🤖 Generated with Claude Code

Fixes invalid markdown URL generation when a static page is configured
as the front page (Settings → Reading). Previously, get_permalink()
would return the site root (e.g., https://example.com/), and appending
.md resulted in https://example.com.md, which appears to be an invalid
TLD rather than a markdown file.

Changes:
- Added convert_to_markdown_url() method to both AlternateLinkHandler
  and RewriteHandler to detect front page permalinks and convert them
  to /index.md instead of .md
- Updated parse_markdown_url() to handle /index.md requests and resolve
  them to the page_on_front option
- Added dedicated rewrite rule for index.md to ensure proper routing

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where setting a static page as the WordPress front page resulted in an invalid markdown URL (e.g., https://example.com.md instead of https://example.com/index.md). The fix introduces special handling for the front page by detecting when a permalink matches the site root and converting it to /index.md.

Changes:

  • Added convert_to_markdown_url() methods in both AlternateLinkHandler and RewriteHandler to handle front page URL generation
  • Added special routing logic in parse_markdown_url() to resolve /index.md requests to the configured front page
  • Added a dedicated rewrite rule for index.md pattern

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/Router/RewriteHandler.php Added rewrite rule for index.md, special handling in parse_markdown_url() for front page resolution, new convert_to_markdown_url() method, and updated Accept header negotiation to use the new method
src/Discovery/AlternateLinkHandler.php Added convert_to_markdown_url() method for generating proper markdown URLs in alternate link tags

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 374 to 386
private function convert_to_markdown_url(string $permalink): string {
// Normalize both URLs by removing trailing slashes for comparison
$normalized_permalink = rtrim($permalink, '/');
$normalized_home = rtrim(home_url('/'), '/');

// If this is the front page, use /index.md
if ($normalized_permalink === $normalized_home) {
return home_url('/index.md');
}

// Otherwise, append .md to the permalink
return $normalized_permalink . '.md';
}
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The convert_to_markdown_url() method is duplicated between RewriteHandler and AlternateLinkHandler classes. This is a maintainability concern - any future changes to the URL conversion logic would need to be made in two places, increasing the risk of inconsistencies.

Consider extracting this method to a shared utility class or trait that both classes can use. For example, create a MarkdownUrlConverter utility class or a trait that both handlers can use.

Copilot uses AI. Check for mistakes.
Comment on lines 102 to 109
// Handle /index.md as the front page
if ($matches[1] === 'index') {
$page_on_front = get_option('page_on_front');
if ($page_on_front) {
$post_id = (int) $page_on_front;
} else {
// No static front page set - let WordPress show its normal behavior
return;
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a potential collision if a user creates an actual page with the slug "index". When visiting /index.md, the current logic will always treat it as the front page (lines 103-110) rather than checking if there's an actual page with slug "index".

The logic should first attempt to resolve "index" as a regular page slug, and only fall back to treating it as the front page if no such page exists. Suggested fix:

// Handle /index.md - could be front page or a page with slug "index"
if ($matches[1] === 'index') {
    // First try to resolve as a regular page
    $clean_url = home_url('/index');
    $post_id = url_to_postid($clean_url);
    
    // If no page with slug "index" exists, treat as front page
    if (!$post_id) {
        $page_on_front = get_option('page_on_front');
        if ($page_on_front) {
            $post_id = (int) $page_on_front;
        } else {
            return;
        }
    }
}
Suggested change
// Handle /index.md as the front page
if ($matches[1] === 'index') {
$page_on_front = get_option('page_on_front');
if ($page_on_front) {
$post_id = (int) $page_on_front;
} else {
// No static front page set - let WordPress show its normal behavior
return;
// Handle /index.md - could be front page or a page with slug "index"
if ($matches[1] === 'index') {
// First try to resolve as a regular page with slug "index"
$clean_url = home_url('/index');
$post_id = url_to_postid($clean_url);
// If no page with slug "index" exists, treat as front page
if (!$post_id) {
$page_on_front = get_option('page_on_front');
if ($page_on_front) {
$post_id = (int) $page_on_front;
} else {
// No static front page set - let WordPress show its normal behavior
return;
}

Copilot uses AI. Check for mistakes.
Joost de Valk and others added 2 commits February 15, 2026 23:37
- Extract convert_to_markdown_url() to shared UrlConverter utility class
  to eliminate code duplication between RewriteHandler and AlternateLinkHandler
- Fix edge case where a page with slug "index" would be inaccessible at
  /index.md - now checks for actual "index" page before falling back to
  front page logic

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@jdevalk
Copy link
Member Author

jdevalk commented Feb 17, 2026

Consolidation note: treating this as the primary PR for static front-page markdown URL behavior (supersedes #6). Recommended to merge early before rewrite refactors.

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.

Static front page produces invalid .md URL (e.g. example.com.md)

2 participants

Comments