Add Netlify _redirects for pretty URLs with proper 301 redirects
This commit is contained in:
parent
9af0acc294
commit
f478bd6f0a
4 changed files with 46 additions and 17 deletions
27
scripts/generate-redirects.js
Normal file
27
scripts/generate-redirects.js
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
// Generate _redirects file for pretty URLs
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const contentDir = path.join(__dirname, '../src/content/blog');
|
||||
|
||||
const redirects = [
|
||||
'# redirects for pretty URLs - generated automatically',
|
||||
''
|
||||
];
|
||||
|
||||
// Read all blog post directories
|
||||
const entries = fs.readdirSync(contentDir, { withFileTypes: true });
|
||||
for (const entry of entries) {
|
||||
if (entry.isDirectory()) {
|
||||
const slug = entry.name;
|
||||
redirects.push(`/${slug} /blog/${slug} 301`);
|
||||
}
|
||||
}
|
||||
|
||||
// Write _redirects file
|
||||
const outputPath = path.join(__dirname, '../public/_redirects');
|
||||
fs.writeFileSync(outputPath, redirects.join('\n') + '\n');
|
||||
|
||||
console.log(`Generated ${redirects.length - 2} redirects`);
|
||||
Loading…
Add table
Add a link
Reference in a new issue