Class UrlRewriter
java.lang.Object
io.leandev.appfuse.docs.UrlRewriter
URL 替換工具
用於將 HTML 中的絕對 URL(canonical、og:url 等)進行替換。
使用範例
String html = UrlRewriter.rewriteHtml(
html,
"http://localhost:2000",
"https://your-domain.com/app-docs"
);
String sitemap = UrlRewriter.rewriteSitemap(
sitemap,
"http://localhost:2000",
"https://your-domain.com/app-docs"
);
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String建構完整的目標 URLstatic StringnormalizeUrl(String url) 正規化 URL,移除結尾的斜線static StringrewriteHtml(String html, String sourceUrl, String targetUrl) 替換 HTML 中的絕對 URLstatic StringrewriteSitemap(String sitemap, String sourceUrl, String targetUrl) 替換 sitemap.xml 中的 URL
-
Constructor Details
-
UrlRewriter
public UrlRewriter()
-
-
Method Details
-
rewriteHtml
替換 HTML 中的絕對 URL
涵蓋 SEO 與社群分享所需的各種 URL 出現位置:
<link rel="canonical"><meta property="og:url">、<meta property="og:image"><meta name="twitter:url">、<meta name="twitter:image"><link rel="alternate">(hreflang 等)<script type="application/ld+json">區段內的 URL
- Parameters:
html- HTML 內容sourceUrl- 原始 URL(建置時使用)targetUrl- 目標 URL(部署時使用)- Returns:
- 替換後的 HTML
-
rewriteSitemap
-
buildUrl
建構完整的目標 URL
將 siteUrl 與 basename 組合。 範例:
https://example.com+/docs→https://example.com/docshttps://example.com/cfoh+/docs→https://example.com/cfoh/docs(siteUrl 含可選專案前綴)
- Parameters:
siteUrl- 站台網域(如https://example.com),支援含可選專案前綴 path(如https://example.com/cfoh)basename- 模組路徑(如/docs/)- Returns:
- 完整 URL
-
normalizeUrl
-