You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP实现多语言URL切换:将MD语言URL替换为RU语言

实现多语言URL切换的Alternate Link配置

Alright, let's get this sorted for your multi-language site setup. Based on your URL rules (md as default, with /md/ or no prefix for md, /ru/ for Russian), here's how to properly implement the language switch alternate links when users are in the md language environment:

Static Implementation (For Static Pages)

If you're working with static HTML files, you can hardcode the alternate links directly. For the example URL http://site.ru/news/new-news-article (default md) or http://site.ru/md/news/new-news-article, add these tags in your page's <head>:

<!-- Link to the Russian (ru) version of the current page -->
<link rel="alternate" hreflang="ru" href="http://site.ru/ru/news/new-news-article">

<!-- Self-referential link for md language, plus default language indicator -->
<link rel="alternate" hreflang="md" href="http://site.ru/news/new-news-article">
<link rel="alternate" hreflang="x-default" href="http://site.ru/news/new-news-article">

What each part does:

  • rel="alternate": Tells browsers and search engines this is an alternative language version of the current page.
  • hreflang="ru": Explicitly marks the link as the Russian language variant. Notice we just add the /ru/ prefix to the default md URL (or replace /md/ with /ru/ if the current URL has the md prefix).
  • hreflang="md" & hreflang="x-default: The x-default tag is crucial here—it tells search engines that md is your site's default language, which helps avoid indexing confusion. The md hreflang link points to the canonical md URL (without the /md/ prefix, since that's your default).

Dynamic Implementation (For Server-Side Rendered Sites)

If you're using a backend framework (like PHP, Node.js, etc.), you can generate these links dynamically to avoid manually updating every page. Here's a quick example with PHP:

<?php
// Grab the current request path (e.g., "/news/new-news-article" or "/md/news/new-news-article")
$currentPath = $_SERVER['REQUEST_URI'];

// Generate the Russian path: replace "/md/" with "/ru/", or add "/ru/" if no prefix
$ruPath = preg_replace('/^\/md\/|^\/?/', '/ru/', $currentPath);

// Generate the canonical md path: remove any language prefix (/md/ or /ru/)
$mdCanonicalPath = preg_replace('/^\/md\/|^\/ru\/|^\/?/', '/', $currentPath);
?>

<link rel="alternate" hreflang="ru" href="http://site.ru<?php echo $ruPath; ?>">
<link rel="alternate" hreflang="md" href="http://site.ru<?php echo $mdCanonicalPath; ?>">
<link rel="alternate" hreflang="x-default" href="http://site.ru<?php echo $mdCanonicalPath; ?>">

This code automatically adjusts the URLs no matter which md URL the user is on (with or without the /md/ prefix), so you don't have to write custom links for every page.

Key Notes

  • Always make sure the alternate URLs point to the actual corresponding pages—broken links here will hurt SEO and user experience.
  • If you have more pages, the dynamic approach is way more scalable than static hardcoding.
  • Don't skip the x-default tag—it's a signal to search engines about your default language, which helps with international indexing.

内容的提问来源于stack exchange,提问作者Jadasdas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:21:19