<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>next.js | ServerAvatar</title>
	<atom:link href="https://serveravatar.com/tag/next-js/feed/" rel="self" type="application/rss+xml" />
	<link>https://serveravatar.com</link>
	<description>The first, Fully Hybrid Cloud Hosting Solution.</description>
	<lastBuildDate>Wed, 24 Sep 2025 10:30:26 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://serveravatar.com/wp-content/uploads/2025/02/cropped-favicon-32x32.png</url>
	<title>next.js | ServerAvatar</title>
	<link>https://serveravatar.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Optimize Next.js robots.txt for Better SEO</title>
		<link>https://serveravatar.com/next-js-robots-txt-guide/</link>
		
		<dc:creator><![CDATA[Smit Pipaliya]]></dc:creator>
		<pubDate>Sun, 20 Jul 2025 02:35:00 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Optimisation]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[How to Optimize Next.js robots.txt]]></category>
		<category><![CDATA[next.js]]></category>
		<category><![CDATA[Optimize Next.js]]></category>
		<category><![CDATA[PHP Hosting]]></category>
		<category><![CDATA[robot]]></category>
		<category><![CDATA[robots.txt]]></category>
		<category><![CDATA[SEo]]></category>
		<guid isPermaLink="false">https://serveravatar.com/?p=26325</guid>

					<description><![CDATA[<p>So you’ve built your awesome Next.js website, now it’s time to make sure next.js robots.txt is set up correctly. But what if search engines aren’t seeing it the way you want? That’s where the robots.txt file comes in, and it’s more important than most developers realize. Whether you want to improve your Next.js SEO, reduce [&#8230;]</p>
<p>The post <a href="https://serveravatar.com/next-js-robots-txt-guide/">How to Optimize Next.js robots.txt for Better SEO</a> first appeared on <a href="https://serveravatar.com">ServerAvatar</a>.</p>]]></description>
										<content:encoded><![CDATA[<figure class="wp-block-image size-large"><a href="https://serveravatar.com?ref=blog" target="_blank" rel=" noreferrer noopener"><img fetchpriority="high" decoding="async" width="1024" height="127" src="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-1024x127.png" alt="Blog banner - ServerAvatar" class="wp-image-28182" srcset="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-1024x127.png 1024w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-300x37.png 300w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-768x95.png 768w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-1536x190.png 1536w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-3-2048x253.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>So you’ve built your awesome Next.js website, now it’s time to make sure <strong>next.js robots.txt</strong> is set up correctly. But what if search engines aren’t seeing it the way you want? That’s where the robots.txt file comes in, and it’s more important than most developers realize.</p>



<p>Whether you want to improve your Next.js SEO, reduce unnecessary crawling, or block bots from accessing private routes like /admin or /api, having the correct Next.js robots.txt setup is essential.</p>



<p>In this easy-to-follow guide, we’ll show you exactly how to create and configure a robots.txt file in a Next.js app, step-by-step, with no technical jargon. Let’s help search engines crawl your site the right way.</p>



<h2 class="wp-block-heading">What is robots.txt?</h2>



<p>The <code>robots.txt</code> The file is like a polite bouncer at a club. It tells search engine bots which parts of your website they’re allowed to visit and which areas are off-limits.</p>



<h3 class="wp-block-heading">Basic Syntax of robots.txt</h3>



<p>Here’s a simple example:</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Plaintext</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>User-agent: *
Disallow: /admin/
Allow: /
Sitemap: https://example.com/sitemap.xml</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">User-agent: *</span></span>
<span class="line"><span style="color: #D4D4D4">Disallow: /admin/</span></span>
<span class="line"><span style="color: #D4D4D4">Allow: /</span></span>
<span class="line"><span style="color: #D4D4D4">Sitemap: https://example.com/sitemap.xml</span></span></code></pre></div>



<ul class="wp-block-list">
<li><code>User-agent</code>: Specifies which bot (Googlebot, Bingbot, etc.)</li>



<li><code>Disallow</code>: Blocks access to paths</li>



<li><code>Allow</code>: Grants access even in disallowed folders</li>



<li><code>Sitemap</code>: Points bots to your sitemap</li>
</ul>



<h2 class="wp-block-heading">Why robots.txt Matters for SEO</h2>



<p>If Google crawls pages it shouldn’t (like <code>/cart</code> or <code>/dashboard</code>), you waste crawl budget and risk exposing sensitive content. robots.txt is how you <strong>take control</strong> of what bots see.</p>



<h3 class="wp-block-heading">Top Reasons You Need One:</h3>



<ul class="wp-block-list">
<li>Prevent indexing of non-public pages</li>



<li>Improve crawl efficiency</li>



<li>Avoid duplicate content issues</li>



<li>Help Google focus on your money pages</li>
</ul>



<h2 class="wp-block-heading">How Next.js Handles SEO</h2>



<p>Next.js is fantastic for performance and user experience. But it doesn’t create a robots.txt file by default.</p>



<h3 class="wp-block-heading">Why This Matters</h3>



<p>Since your site might be dynamic (e.g., server-side rendered or hybrid), you need to be <strong>intentional</strong> about robots.txt.</p>



<h2 class="wp-block-heading">Setting Up robots.txt in Next.js</h2>



<p>There are two main ways to add a robots.txt file in Next.js:</p>



<ol class="wp-block-list">
<li><strong>Static robots.txt</strong></li>



<li><strong>Dynamic robots.txt via API route</strong></li>
</ol>



<p>Let’s explore both.</p>



<h3 class="wp-block-heading">Method 1 – Static robots.txt File</h3>



<p>This is the easiest and most common approach.</p>



<h4 class="wp-block-heading"><strong>Steps:</strong></h4>



<ol class="wp-block-list">
<li>Go to your <a href="https://nextjs.org/" target="_blank" rel="noopener nofollow" title="">Next.js</a> project root.</li>



<li>Navigate to the <code>/public</code> directory.</li>



<li>Create a new file named <code>robots.txt</code>.</li>
</ol>



<h4 class="wp-block-heading"><strong>Example:</strong></h4>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Plaintext</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>User-agent: *
Disallow: /admin/
Sitemap: https://yourdomain.com/sitemap.xml</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">User-agent: *</span></span>
<span class="line"><span style="color: #D4D4D4">Disallow: /admin/</span></span>
<span class="line"><span style="color: #D4D4D4">Sitemap: https://yourdomain.com/sitemap.xml</span></span></code></pre></div>



<p>Once deployed, this will be available at:<br><code>https://yourdomain.com/robots.txt</code></p>



<h4 class="wp-block-heading">When to Use:</h4>



<ul class="wp-block-list">
<li>Your rules don’t change by environment</li>



<li>You don’t need dynamic paths</li>
</ul>



<h3 class="wp-block-heading">Method 2 – Dynamic robots.txt with API Route</h3>



<p>Need more control? Maybe you want to serve different robots.txt content in development vs production? That’s where API routes shine.</p>



<h4 class="wp-block-heading"><strong>Steps:</strong></h4>



<ol class="wp-block-list">
<li>Create a new file under <code>pages/api/robots.ts</code> or <code>.js</code></li>
</ol>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">TSX</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>export default function handler(req, res) {
  res.setHeader('Content-Type', 'text/plain');
  res.send(
`User-agent: *
Disallow: /private/
Sitemap: https://yourdomain.com/sitemap.xml`
  );
}</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #C586C0">export</span><span style="color: #D4D4D4"> </span><span style="color: #C586C0">default</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">function</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">handler</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">req</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">res</span><span style="color: #D4D4D4">) {</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #9CDCFE">res</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">setHeader</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&#39;Content-Type&#39;</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&#39;text/plain&#39;</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #9CDCFE">res</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">send</span><span style="color: #D4D4D4">(</span></span>
<span class="line"><span style="color: #CE9178">`User-agent: *</span></span>
<span class="line"><span style="color: #CE9178">Disallow: /private/</span></span>
<span class="line"><span style="color: #CE9178">Sitemap: https://yourdomain.com/sitemap.xml`</span></span>
<span class="line"><span style="color: #D4D4D4">  );</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span></code></pre></div>



<ol start="2" class="wp-block-list">
<li>In your <code>next.config.js</code>, redirect <code>/robots.txt</code> to the API route:</li>
</ol>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">JavaScript</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>module.exports = {
  async rewrites() {
    return &#91;
      {
        source: '/robots.txt',
        destination: '/api/robots'
      }
    &#93;;
  }
}</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #4EC9B0">module</span><span style="color: #D4D4D4">.</span><span style="color: #4EC9B0">exports</span><span style="color: #D4D4D4"> = {</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #569CD6">async</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">rewrites</span><span style="color: #D4D4D4">() {</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> &#91;</span></span>
<span class="line"><span style="color: #D4D4D4">      {</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">source:</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&#39;/robots.txt&#39;</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #9CDCFE">destination:</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&#39;/api/robots&#39;</span></span>
<span class="line"><span style="color: #D4D4D4">      }</span></span>
<span class="line"><span style="color: #D4D4D4">    &#93;;</span></span>
<span class="line"><span style="color: #D4D4D4">  }</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span></code></pre></div>



<p>Now you can <strong>generate robots.txt dynamically</strong>. Super useful for multi-environment setups!</p>



<h3 class="wp-block-heading"><strong>Dynamic vs Static – Which One Should You Use?</strong></h3>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Static</th><th>Dynamic</th></tr></thead><tbody><tr><td>Easy to implement</td><td>Requires setup</td></tr><tr><td>Best for small sites</td><td>Great for scaling &amp; customization</td></tr><tr><td>One-size-fits-all</td><td>Adapts by environment or user</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">Using Next.js with next-sitemap for SEO Automation</h2>



<p>Want to automate everything? <code>next-sitemap</code> is your best friend. It can <strong>generate sitemap.xml and robots.txt automatically</strong> &#8211; saving tons of time.</p>



<p>But before you dive into automation, you might want to <strong>manually generate and test your robots.txt</strong> file to make sure you understand the basics and get the right output.</p>



<p>👉 <strong>Use this free online tool:</strong> <a class="" href="https://serveravatar.com/robots-txt-generator/">ServerAvatar Robots.txt Generator</a><br>It’s a quick and simple way to build a clean, SEO-friendly <code>robots.txt</code> in seconds.</p>



<h3 class="wp-block-heading"><strong>Install next-sitemap:</strong></h3>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>npm install next-sitemap</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">npm</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">install</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">next-sitemap</span></span></code></pre></div>



<p>Create a config file: <code>next-sitemap.config.js</code></p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">JavaScript</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>module.exports = {
  siteUrl: 'https://yourdomain.com',
  generateRobotsTxt: true,
  robotsTxtOptions: {
    policies: &#91;
      { userAgent: '*', allow: '/' },
    &#93;,
    additionalSitemaps: &#91;
      'https://yourdomain.com/my-custom-sitemap.xml',
    &#93;,
  },
}</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #4EC9B0">module</span><span style="color: #D4D4D4">.</span><span style="color: #4EC9B0">exports</span><span style="color: #D4D4D4"> = {</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #9CDCFE">siteUrl:</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&#39;https://yourdomain.com&#39;</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #9CDCFE">generateRobotsTxt:</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">true</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #9CDCFE">robotsTxtOptions:</span><span style="color: #D4D4D4"> {</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #9CDCFE">policies:</span><span style="color: #D4D4D4"> &#91;</span></span>
<span class="line"><span style="color: #D4D4D4">      { </span><span style="color: #9CDCFE">userAgent:</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&#39;*&#39;</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">allow:</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&#39;/&#39;</span><span style="color: #D4D4D4"> },</span></span>
<span class="line"><span style="color: #D4D4D4">    &#93;,</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #9CDCFE">additionalSitemaps:</span><span style="color: #D4D4D4"> &#91;</span></span>
<span class="line"><span style="color: #D4D4D4">      </span><span style="color: #CE9178">&#39;https://yourdomain.com/my-custom-sitemap.xml&#39;</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">    &#93;,</span></span>
<span class="line"><span style="color: #D4D4D4">  },</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span></code></pre></div>



<h3 class="wp-block-heading"><strong>Update package.json:</strong></h3>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">JSON</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>"scripts": {
  "postbuild": "next-sitemap"
}</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #CE9178">&quot;scripts&quot;</span><span style="color: #D4D4D4">: {</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #9CDCFE">&quot;postbuild&quot;</span><span style="color: #D4D4D4">: </span><span style="color: #CE9178">&quot;next-sitemap&quot;</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span></code></pre></div>



<p>After building your app, it will <strong>auto-generate <code>robots.txt</code> and <code>sitemap.xml</code></strong> under <code>/public</code>.</p>



<h2 class="wp-block-heading">Best Practices for robots.txt in Next.js</h2>



<ul class="wp-block-list">
<li>Always <strong>include your sitemap</strong></li>



<li>Don’t block essential JS or CSS files</li>



<li>Use different rules for staging vs production</li>



<li>Avoid <code>Disallow: /</code> unless under construction</li>
</ul>



<h3 class="wp-block-heading">Tips for Handling Crawlers and Bots</h3>



<ul class="wp-block-list">
<li>Use <code>User-agent: Googlebot</code> for Google-only rules</li>



<li>Block known bad bots if needed</li>



<li>Monitor crawl errors via Google Search Console</li>
</ul>



<figure class="wp-block-image size-large"><a href="https://serveravatar.com?ref=blog" target="_blank" rel=" noreferrer noopener"><img decoding="async" width="1024" height="127" src="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-1024x127.png" alt="Blog banner - ServerAvatar" class="wp-image-28184" srcset="https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-1024x127.png 1024w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-300x37.png 300w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-768x95.png 768w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-1536x190.png 1536w, https://serveravatar.com/wp-content/uploads/2025/09/728_90-Banner-1-2048x254.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<h2 class="wp-block-heading">Validating and Testing Your robots.txt</h2>



<h3 class="wp-block-heading">Tools You Can Use:</h3>



<ul class="wp-block-list">
<li><a>Google robots.txt Tester</a></li>



<li>Use <code>curl</code>:</li>
</ul>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Bash</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>curl https://yourdomain.com/robots.txt</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #DCDCAA">curl</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">https://yourdomain.com/robots.txt</span></span></code></pre></div>



<ul class="wp-block-list">
<li>Browser testing works too!</li>
</ul>



<h2 class="wp-block-heading">Common Mistakes to Avoid</h2>



<ul class="wp-block-list">
<li>Blocking <code>/static/</code> or <code>_next/</code> &#8211; your site might break!</li>



<li>Leaving <code>Disallow: /</code> in production</li>



<li>Forgetting <code>Sitemap:</code> directive</li>



<li>Using uppercase file names (use <code>robots.txt</code>, not <code>Robots.TXT</code>)</li>
</ul>



<h2 class="wp-block-heading">Advanced Tips for Large-Scale Projects</h2>



<ul class="wp-block-list">
<li><strong>Multi-language support?</strong> Serve dynamic robots.txt per locale</li>



<li><strong>Different environments?</strong> Use environment variables</li>



<li><strong>Subdomains?</strong> Each subdomain should have its own robots.txt</li>
</ul>



<h2 class="wp-block-heading">Conclusion</h2>



<p>Getting your robots.txt right might seem like a small thing &#8211; but it can make a big difference in how search engines see (or ignore) your content. With Next.js, you&#8217;ve got powerful tools to make this setup easy, whether you go the static route, API-based, or automated with <code>next-sitemap</code>.</p>



<p>Now that you’ve got the full picture, it’s time to take control of your SEO game. Go build, block, and boost!</p>



<h2 class="wp-block-heading">FAQs</h2>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question">Can I use environment variables in robots.txt?</h3><div class="aioseo-faq-block-answer">
<p>Yes, if you&#8217;re generating it dynamically via API route. You can pull from <code>process.env</code>.</p>
</div></div>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question">How often does Google crawl my robots.txt?</h3><div class="aioseo-faq-block-answer">
<p>Google typically fetches it every 24 hours or more often if changes are detected.</p>
</div></div>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question">Can I have multiple robots.txt files?</h3><div class="aioseo-faq-block-answer">
<p>No, only one per domain. But you can dynamically generate it based on logic.</p>
</div></div>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question">Does robots.txt affect ranking directly?</h3><div class="aioseo-faq-block-answer">
<p>Not directly, but it affects what gets indexed &#8211; which absolutely impacts rankings.</p>
</div></div>



<div data-schema-only="false" class="wp-block-aioseo-faq"><h3 class="aioseo-faq-block-question">Is it okay to block <code>/api</code> routes?</h3><div class="aioseo-faq-block-answer">
<p>Yes, usually you should block them unless they return public content.</p>
</div></div>



<p></p><p>The post <a href="https://serveravatar.com/next-js-robots-txt-guide/">How to Optimize Next.js robots.txt for Better SEO</a> first appeared on <a href="https://serveravatar.com">ServerAvatar</a>.</p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
