Skip to content

Conversation

Copy link

Copilot AI commented Sep 4, 2025

When organization names or location details change, the location URL slugs change accordingly, but old URLs were showing 404 errors instead of redirecting to the new URLs. This creates broken links for users and impacts SEO.

Problem

The issue occurred when:

  • Organization names changed (e.g., "Montefiore Medical Center — Albert Einstein Wellness Center" → "Montefiore Einstein")
  • Location addresses changed, affecting neighborhood names in URLs
  • Any combination of the above

For example:

  • Old URL: /locations/montefiore-medical-center-albert-einstein-wellness-center-port-morris
  • New URL: /locations/montefiore-einstein-port-morris
  • Result: 404 error instead of redirect

Solution

Added Next.js middleware (src/middleware.ts) that automatically handles 301 redirects for changed location URLs:

  1. Intercepts location requests: Monitors requests to /locations/{slug} patterns
  2. Checks existence: Makes HEAD request to verify if location exists
  3. Smart search: If 404, extracts organization/location terms from old slug and searches for matching locations
  4. Similarity matching: Uses string similarity algorithm to find best matching location (threshold > 0.3)
  5. 301 redirect: Redirects to new URL preserving query parameters and fragments

Key Features

  • SEO-friendly: Uses proper 301 redirects to preserve search rankings
  • Performance-optimized: Includes request timeouts and limits search attempts
  • Error-resilient: Graceful fallback to normal 404 if search fails
  • Zero breaking changes: Purely additive, doesn't modify existing code
  • Comprehensive logging: Debug information for monitoring redirect behavior

Test Results

Validated against the reported examples:

  • montefiore-medical-center-albert-einstein-wellness-center-port-morrismontefiore-einstein-port-morris
  • montefiore-medical-center-albert-einstein-wellness-center-wakefieldmontefiore-einstein-wakefield
  • montefiore-medical-center-morris-heightsmontefiore-einstein-*
  • montefiore-medical-center-norwoodmontefiore-einstein-norwood-111-east-210th-street

Fixes #242.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • fonts.googleapis.com
    • Triggering command: /usr/local/bin/node /home/REDACTED/work/yourpeer.nyc/yourpeer.nyc/node_modules/next/dist/compiled/jest-worker/processChild.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copy link
Author

Copilot AI commented Sep 4, 2025

@jbeard4 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

@jbeard4 jbeard4 temporarily deployed to yourpeer-copilot-fix-24-lagw8p September 4, 2025 23:34 Inactive
Copilot AI changed the title [WIP] Links changed by org or location changes are not forwarding to a new link, and invalidating the old link Add middleware to handle 301 redirects for changed location URLs Sep 4, 2025
Copilot AI requested a review from jbeard4 September 4, 2025 23:36
@jbeard4
Copy link
Member

jbeard4 commented Sep 19, 2025

@shakilhossain1 Is this PR old? Do we still need a middleware? I thought we fixed this issue here. Please let me know if I should close this PR.

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.

Links changed by org or location changes are not forwarding to a new link, and invalidating the old link

2 participants