SEO for Laravel: titles, canonicals, sitemap and what to check
Laravel SEO in code: one Blade layout for the title and canonical, a sitemap route with real dates, the right redirect status, and tests that catch a broken deploy.
SEO for Laravel is mostly four things: one Blade layout that prints the title, description and canonical for every page, a sitemap route that reports real dates, redirects that answer 301 and not 302, and a few tests so a deploy cannot quietly break any of it. Laravel renders pages on the server, so the tags you write are the tags Google reads. It ships with none of them written for you.
This site runs on Laravel, so the examples below are the patterns it uses, trimmed. Each framework claim is from the Laravel docs, linked at the end.
TL;DR:
- Put
<title>, the description and the canonical in the layout, with@yieldand a default, so no page ships without them. - Build the canonical from
url()->current(), which leaves the query string out. - Serve the sitemap from a route, and give
lastmodthe date the content changed.updated_atis not that date. Route::redirectanswers 302 unless you say otherwise. UseRoute::permanentRedirectfor a page that moved.- Write a test per rule. They take a minute each and they run on every deploy.
What does Laravel do for SEO by default?
Laravel sends finished HTML from the server, which is the part that matters most: a crawler that runs no JavaScript sees the whole page. Past that, nothing is set up. There is no title helper, no canonical, no sitemap and no robots rule beyond the starter public/robots.txt. That is fine. Each one is a few lines, and you get to see exactly what is printed.
If you render pages with Livewire or Blade, everything here applies as written. If the front end is a separate JavaScript app that only calls Laravel as an API, the head tags are that app's job, and this guide does not cover it.
How do you set the title and meta description in Blade?
Print them in the layout with @yield, and give each one a default. The docs say "The @yield directive also accepts a default value as its second parameter", which means a page that forgets its title still gets a sensible one.
{{-- resources/views/layouts/marketing.blade.php --}}
<head>
<title>@yield('title', 'Example: invoicing for freelancers')</title>
<meta name="description" content="@yield('description', 'Send an invoice in a minute.')">
<link rel="canonical" href="@yield('canonical', rtrim(url()->current(), '/') ?: url('/'))">
</head>
A page then sets its own:
{{-- resources/views/pages/pricing.blade.php --}}
@extends('layouts.marketing')
@section('title', 'Pricing · Example')
@section('description', 'Three plans. Every feature in each one.')
For a page built from a database row, pass the model's own fields: @section('title', $post->title). Blade escapes them for you. The docs say {{ }} statements "are automatically sent through PHP's htmlspecialchars function", so a title with a quote or an ampersand in it cannot break the tag.
A default is a safety net, not a plan. Two pages with the same default title compete with each other in search, so the test further down fails any page that still shows it.
How do you set a canonical URL in Laravel?
Build it from url()->current() in the layout, as in the example above, and let a page override it when it has a better answer. The docs describe url()->current() as the current URL "without the query string", which is what you want: /pricing?ref=newsletter and /pricing are one page and should name one canonical.
Trailing slashes need a decision too. Laravel matches /pricing/ to the same route as /pricing, and whether the slash form redirects depends on your web server, not on Laravel. On this site, which runs behind Caddy, both forms answer 200, so the layout strips the slash with rtrim and both print the same canonical. If your server redirects one form to the other, you can drop the rtrim. Check with curl -I before you decide.
Pages that live at a known address should say so plainly. A blog post sets its own:
@section('canonical', $post->url())
Then look at the scheme. The url helper builds links from the incoming request, and the docs warn that behind a load balancer that terminates TLS "you may notice your application sometimes does not generate HTTPS links". The fix is the trusted proxies setting. Without it, a whole site can print http:// canonicals while every page loads over HTTPS.
How do you add a sitemap to a Laravel app?
Write a route that returns XML. A package is optional: the whole thing is one controller method and one Blade file. Use response() so you can set the content type.
// routes/web.php
Route::get('/sitemap.xml', [SitemapController::class, 'index']);
// app/Http/Controllers/SitemapController.php
public function index(): Response
{
$entries = [
['loc' => url('/'), 'lastmod' => '2026-11-01'],
['loc' => url('/pricing'), 'lastmod' => '2026-10-17'],
];
foreach (Post::published()->get() as $post) {
$entries[] = [
'loc' => $post->url(),
'lastmod' => $post->content_changed_at->toDateString(),
];
}
return response(view('sitemap', ['entries' => $entries])->render(), 200)
->header('Content-Type', 'application/xml; charset=UTF-8');
}
{{-- resources/views/sitemap.blade.php --}}
{!! '<' . '?xml version="1.0" encoding="UTF-8"?' . '>' !!}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@foreach ($entries as $e)
<url>
<loc>{{ $e['loc'] }}</loc>
<lastmod>{{ $e['lastmod'] }}</lastmod>
</url>
@endforeach
</urlset>
The first line of that template looks odd on purpose. Blade compiles to a PHP file, and a plain <?xml puts a <? in it, which PHP reads as an opening tag when short tags are switched on. Printing the declaration as a string avoids the question.
Then point crawlers at it from public/robots.txt:
User-agent: *
Allow: /
Sitemap: https://example.com/sitemap.xml
Leave changefreq and priority out. Google's sitemap docs say it ignores both.
Try it
Point an SEO agent at your own site, not just an essay about one.
QueryScope holds the plan for your site, reads your real Search Console, and hands the coding agent you already have one change at a time to rank for the searches you decided to win. From $49/month, everything included.
Why is updated_at the wrong date for lastmod?
Because Eloquent moves it on every save. The docs say Eloquent "will automatically set these column's values when models are created or updated", so fixing a typo in an internal note, or a job that touches a counter, changes updated_at on a page no reader would call changed.
Google only trusts the field while it is true. Its docs say it uses lastmod "if it's consistently and verifiably" accurate, and that the value "should reflect the date and time of the last significant update to the page". A sitemap where every row moved last night teaches Google to ignore the dates on all of them.
So keep a column of your own, such as content_changed_at, and set it only when the text a visitor reads changes. For static pages with no model behind them, a date written by hand beside the URL is fine. That is what this site does for its marketing pages, and the rule for when to move one is the same. How Google uses lastmod goes through the cases.
Which redirect status does Laravel send?
302, unless you ask for 301. The docs are direct about it: "By default, Route::redirect returns a 302 status code." A 302 says the move is temporary, so use the permanent form for a page that moved for good.
Route::redirect('/old-pricing', '/pricing'); // 302
Route::redirect('/old-pricing', '/pricing', 301); // 301
Route::permanentRedirect('/old-pricing', '/pricing'); // 301
The same care applies to pages you removed. A URL that has nothing to redirect to should answer with an error status, not with a friendly page and a 200. Laravel's abort helper does that from anywhere, and a view at resources/views/errors/404.blade.php gives the error its own page while keeping the status. For a page that is gone for good, this site answers 410:
Route::get('/glossary/{slug}', fn () => abort(410));
If the app also serves an API, give unmatched API paths a JSON answer. A script that asks for /api/missing cannot do anything with an HTML page.
Which SEO checks belong in your test suite?
The ones a deploy can break without anyone seeing: the status of a page, its title, its canonical, a stray noindex, and the sitemap. Laravel's HTTP tests make each one a few lines.
test('the pricing page is indexable and names itself', function () {
$response = $this->get('/pricing');
$response->assertStatus(200);
$response->assertSee('<title>Pricing · Example</title>', false);
$response->assertSee('<link rel="canonical" href="' . url('/pricing') . '">', false);
$response->assertDontSee('noindex', false);
});
test('a query string does not change the canonical', function () {
$this->get('/pricing?ref=newsletter')
->assertSee('<link rel="canonical" href="' . url('/pricing') . '">', false);
});
test('the old pricing URL moved for good', function () {
$this->get('/old-pricing')->assertStatus(301)->assertRedirect('/pricing');
});
test('an unknown URL is a real 404', function () {
$this->get('/no-such-page')->assertStatus(404);
});
test('the sitemap lists the pricing page', function () {
$this->get('/sitemap.xml')
->assertStatus(200)
->assertSee('<loc>' . url('/pricing') . '</loc>', false);
});
The false matters. assertSee escapes the string it looks for unless you pass it, and an escaped <title> never matches. Pair an assertDontSee with an assertSee on the same page, as the first test does. A test that only checks something is absent also passes when the page is empty.
A Laravel SEO checklist before a release
- Canonicals print
https://in production. If they printhttp://, set the trusted proxies. - Every page sets its own title and description. No page shows the layout's default.
- Every page prints one canonical, on the production domain, with no query string.
- Moved pages answer 301, removed pages answer 404 or 410.
/sitemap.xmlanswers 200 as XML, androbots.txtpoints at it.lastmodis a content date. No two unrelated pages share last night's.- No
noindexleft over from staging. Search the rendered pages for it. - The tests above run in CI.
What do you change next, and how do you know it worked?
Setup gets a page into Google. It does not tell you which page to work on, or whether last month's title change did anything. Both answers are in Search Console: which searches the site already shows for, where it sits on each, and which pages Google has not indexed.
A Laravel app with its views in the repo suits this, because every change is a commit with a date. SEO tools for developers compares the tools that work from the terminal or the editor instead of a browser tab. QueryScope is one of them: it reads your own Search Console, hands your coding agent one change at a time, and keeps a dated record of each change so you can see what it did.
Sources
- Laravel docs, Blade templates (
@yieldand its default value;@extendsand@section;{{ }}output sent throughhtmlspecialchars). - Laravel docs, URL generation (
url()->current()without the query string;url()->full()with it). - Laravel docs, Routing (
Route::redirectreturning 302 by default;Route::permanentRedirectreturning 301). - Laravel docs, HTTP requests (trusted proxies; HTTPS links behind a load balancer that terminates TLS).
- Laravel docs, HTTP responses (
response()with a status and->header('Content-Type', ...)). - Laravel docs, Error handling (the
aborthelper;resources/views/errors/404.blade.php). - Laravel docs, Eloquent: getting started (
created_atandupdated_atset automatically when models are created or updated). - Laravel docs, HTTP tests (
assertStatus,assertSeeand its escaping,assertDontSee,assertRedirect). - Google Search Central, Build and submit a sitemap (Google ignores
priorityandchangefreq;lastmodused when consistently and verifiably accurate; what counts as a significant update).
Try it
Put an SEO agent in your editor.
Ask your coding agent what to work on. QueryScope reads your real Search Console, holds the searches you decided to win, and hands back one move at a time with the proof it worked. From $49/month, everything included. 1, 3 or 10 sites.