Commit e34ba9d6 authored by jan.koester's avatar jan.koester
Browse files

Canonical/sitemap URLs: don't repeat the blog mount prefix

The staticpage and content plugins set seo.canonicalUrl as
getsiteurl() + buildurl(...). With URL "https://tuxist.de/blog" and
PREFIX "/blog" that gave https://tuxist.de/blog/blog/staticpage/x (404)
on every page; theme.cpp's own fix only covered the fallback og:url.

Add blogi::joinSiteUrl() / Config::absoluteurl(path), which drops the
site URL's path when the prefixed path already starts with it, so both
config styles (URL with or without the mount path) give the right URL.
Use it for canonical links, og:url/og:image, sitemap and llms.txt links,
the old-numeric-id 301 redirect, comment/contact verify mails, the
default plugin sitemap entry and the survey OAuth redirect URI.

Adds tests/siteurl_test.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent d4bf3676
Loading
Loading
Loading
Loading
+1 −1
Changes for plugins/contact/contact.cpp: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -225,7 +225,7 @@ namespace blogi {
                    << createdAt.c_str() << "')";
                Args->database[tid]->exec(sql, res);

                std::string link = Args->config->getsiteurl(); link+="/contact/verify?token=";
                std::string link = Args->config->absoluteurl(Args->config->buildurl("contact/verify")); link+="?token=";
                            link += token.c_str();

                std::string verifyBody;
+7 −7
Changes for plugins/content/content.cpp: 7 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -168,7 +168,7 @@ namespace blogi {
                }
#endif
                sitemap.emplace(
                    Args->config->getsiteurl() + "/content/read/" + res[i][0],
                    Args->config->absoluteurl(Args->config->buildurl("content/read/" + std::string(res[i][0]))),
                                cdate
                );
            }
@@ -183,7 +183,7 @@ namespace blogi {
                blogi::LlmsLink link;
                link.section = "Blog";
                link.title = res[i][1];
                link.url = Args->config->getsiteurl() + "/content/read/" + res[i][0];
                link.url = Args->config->absoluteurl(Args->config->buildurl("content/read/" + std::string(res[i][0])));
                link.description = res[i][2];
                links.push_back(std::move(link));
            }
@@ -301,9 +301,9 @@ namespace blogi {

            std::stringstream href;
            if(tag.empty()){
                href << Args->config->getsiteurl() << Args->config->buildurl("content/index") << "?start="<< start;
                href << Args->config->absoluteurl(Args->config->buildurl("content/index")) << "?start="<< start;
            }else{
                href << Args->config->getsiteurl() << Args->config->buildurl("content/tag/" + tag) << "?start=" << start;
                href << Args->config->absoluteurl(Args->config->buildurl("content/tag/" + tag)) << "?start=" << start;
            }

            blogi::SeoMeta seo;
@@ -354,7 +354,7 @@ namespace blogi {
                    libhttppp::HttpResponse resp;
                    resp.setState(HTTP301);
                    resp.setContentType("text/html");
                    std::string loc = Args->config->getsiteurl() + "/" + Args->config->getprefix() + "/content/read/" + slug;
                    std::string loc = Args->config->absoluteurl(Args->config->buildurl("content/read/" + slug));
                    resp.setHeaderData("location")->push_back(loc);
                    resp.send(curreq,"",0);
                    return;
@@ -559,7 +559,7 @@ namespace blogi {
            }

            std::stringstream href;
            href << Args->config->getsiteurl() << Args->config->buildurl("content/read/" + contentSlug);
            href << Args->config->absoluteurl(Args->config->buildurl("content/read/" + contentSlug));

            std::string description = res[0][7];
            blogi::SeoMeta seo;
@@ -740,7 +740,7 @@ namespace blogi {
            // Send verification email for guest comments
            if(!verifyToken.empty() && Args->smtp){
                try {
                    std::string vurl = Args->config->getsiteurl() + Args->config->buildurl("content/comment/verify") + "?token=" + verifyToken;
                    std::string vurl = Args->config->absoluteurl(Args->config->buildurl("content/comment/verify")) + "?token=" + verifyToken;
                    std::string subject = "Verify your comment";
                    std::string body = "Please verify your comment by clicking this link:\n\n" + vurl + "\n";
                    sendMail(Args->smtp.get(), email.c_str(), subject.c_str(), body);
+3 −3
Changes for plugins/staticpage/static.cpp: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -99,7 +99,7 @@ void blogi::StaticPage::GetSiteMap(const int tid,std::map<libhttppp::HttpUrl,std
    int result = Args->database[tid]->exec(sql,res);
    auto zero_date = std::chrono::year(1970) / std::chrono::month(1) / std::chrono::day(1);
    for(int i=0; i<result; ++i){
        sitemap.emplace(Args->config->getsiteurl()+"/staticpage/"+res[i][0],zero_date);
        sitemap.emplace(Args->config->absoluteurl(Args->config->buildurl("staticpage/"+res[i][0])),zero_date);
    }
}

@@ -163,7 +163,7 @@ void blogi::StaticPage::GetLlmsLinks(const int tid,std::vector<blogi::LlmsLink>
        blogi::LlmsLink link;
        link.section = "Pages";
        link.title = humanizeSlug(res[i][0]);
        link.url = Args->config->getsiteurl()+"/staticpage/"+res[i][0];
        link.url = Args->config->absoluteurl(Args->config->buildurl("staticpage/"+res[i][0]));
        link.description = res[i][1];
        links.push_back(std::move(link));
    }
@@ -667,7 +667,7 @@ bool blogi::StaticPage::Controller(const int tid,libhttppp::HttpRequest &req,lib
        seo.ogDescription = res[0][5];
        seo.ogImage = res[0][6];
        seo.twitterCard = res[0][7];
        seo.canonicalUrl = !res[0][8].empty() ? res[0][8] : (Args->config->getsiteurl() + Args->config->buildurl("staticpage/" + res[0][0]));
        seo.canonicalUrl = !res[0][8].empty() ? res[0][8] : Args->config->absoluteurl(Args->config->buildurl("staticpage/" + res[0][0]));
        seo.robots = res[0][9];
        seo.pageType = "website";

+2 −2
Changes for plugins/survey/survey.cpp: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -2265,7 +2265,7 @@ namespace blogi {
            json_object_object_add(jroot, "nav", jnav);

            // Build OAuth redirect URL
            std::string redirectUri = Args->config->getsiteurl() + Args->config->buildurl("survey/_api/gdrive/callback");
            std::string redirectUri = Args->config->absoluteurl(Args->config->buildurl("survey/_api/gdrive/callback"));
            std::string authUrl;
            if (!_gdriveClientId.empty()) {
                authUrl = "https://accounts.google.com/o/oauth2/v2/auth"
@@ -3125,7 +3125,7 @@ namespace blogi {
            }

            // Exchange code for tokens
            std::string redirectUri = Args->config->getsiteurl() + Args->config->buildurl("survey/_api/gdrive/callback");
            std::string redirectUri = Args->config->absoluteurl(Args->config->buildurl("survey/_api/gdrive/callback"));
            try {
                libhttppp::HttpUrl tokenUrl("https://oauth2.googleapis.com/token");
                libhttppp::HttpClient client(tokenUrl);
+7 −0
Changes for src/conf.h: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@
#include <stdexcept>
#include <string>
#include <vector>

#include "siteurl.h"
#include <map>

#include <confplus/conf.h>
@@ -123,6 +125,11 @@ namespace blogi {
        const std::string &getstartpage() const;

        const std::string    buildurl(const std::string &url) const;
        // Absolute URL of a server path (as returned by buildurl()) on the
        // configured site URL, without repeating the mount prefix.
        std::string          absoluteurl(const std::string &path) const {
            return joinSiteUrl(getsiteurl(), path);
        }

        const std::string &getplgdir(size_t el) const;
        size_t      getplgdirs() const;
Loading