SEO

Search Engine Optimisation (SEO) helps people find your site. Most of it is about good content, but a few technical basics make sure search engines can find, understand and show your pages properly.

Titles and descriptions#

Search results show each page’s <title> and, usually, its <meta name="description">. So far every page has had the same title and an empty description. Make them blocks in _base.html, which pages can fill in:

web/templates/_base.htmljinja
    <meta name="description" content="{% block description %}A blog built with FastAPI.{% endblock %}">

    <title>{% block title %}ExampleForYou{% endblock %}</title>

The blog post template uses the post’s title and teaser:

web/templates/blogpost.htmljinja
{% extends "_base.html" %}

{% block title %}{{ context.post.title }} · ExampleForYou{% endblock %}
{% block description %}{{ context.post.teaser }}{% endblock %}

Pages that only make sense to logged-in users shouldn’t appear in search results at all. Add this to the <head> of admin/_base.html, which the admin and profile pages share:

web/templates/admin/_base.htmlhtml
    <meta name="robots" content="noindex">

robots.txt and sitemap.xml#

robots.txt tells search engines which parts of the site not to crawl, and where the sitemap is. sitemap.xml lists the pages you’d like indexed, so new blog posts are found quickly. Both are generated by a new views file:

web/views/seo_views.pypython
from fastapi import APIRouter, Depends
from fastapi.responses import PlainTextResponse, Response
from sqlmodel import Session

from api.blog_apis import get_blogposts_api
from config.settings import settings
from db.db import get_session_db

router = APIRouter()

@router.get('/robots.txt', response_class=PlainTextResponse, include_in_schema=False)
def robots_txt():
    return (
        "User-agent: *\n"
        "Disallow: /admin\n"
        "Disallow: /profile\n"
        "Disallow: /auth/\n"
        "Disallow: /member\n"
        f"Sitemap: {settings.CONF_MAIN_DOMAIN}/sitemap.xml\n"
    )

@router.get('/sitemap.xml', include_in_schema=False)
def sitemap_xml(session: Session = Depends(get_session_db)):
    domain = settings.CONF_MAIN_DOMAIN
    urls = [f"  <url><loc>{domain}/</loc></url>",
            f"  <url><loc>{domain}/contact</loc></url>"]
    for post in get_blogposts_api(session=session):
        urls.append(f"  <url><loc>{domain}/blog/{post.id}</loc>"
                    f"<lastmod>{post.created_date.date().isoformat()}</lastmod></url>")
    xml = ('<?xml version="1.0" encoding="UTF-8"?>\n'
           '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
           + "\n".join(urls) + "\n</urlset>\n")
    return Response(content=xml, media_type="application/xml")

The sitemap includes every published blog post, using get_blogposts_api with no limit, and the date each was written. The addresses use CONF_MAIN_DOMAIN, so set it to your real domain. Add the router to main.py:

main.pypython
from web.views import home_view, blog_views, contact_view, auth_views, admin_views, profile_views, member_views, seo_views  # UPDATED
main.pypython
    main_app.include_router(seo_views.router, tags=["seo"])  # NEW

One address per page#

If your site answers at both www.exampleforyou.net and exampleforyou.net, or over both http and https, search engines see duplicate pages. Choose one address, and redirect the others to it with a permanent (301) redirect. On OpenShift, the Route’s insecureEdgeTerminationPolicy: Redirect already sends http to https. For www, add a Route for the other hostname, or redirect at your DNS provider.

You can check redirects with curl -I, or a site such as httpstatus.io.

Checking#

Once the site is live, add it to Google Search Console and Bing Webmaster Tools, and submit the sitemap. They show which pages are indexed, and report problems. You can also search for site:your-domain.com to see what’s indexed.