Implement 301 redirects for www to non-www and enhance canonical tag handling

This commit adds 301 redirects in the Apache configuration to redirect traffic from www.tt-tagebuch.de to tt-tagebuch.de for both HTTP and HTTPS. Additionally, it introduces middleware in the backend to dynamically set canonical tags based on the request URL, ensuring proper SEO practices. The request logging middleware has been disabled, and sensitive data handling has been improved in the MyTischtennis model and API logging service, ensuring compliance with data protection regulations. Frontend updates include enhanced descriptions and features in the application, improving user experience and clarity.
This commit is contained in:
Torsten Schulz (local)
2025-11-16 12:08:56 +01:00
parent de36a8ce2b
commit 5b04ed7904
12 changed files with 1201 additions and 146 deletions

View File

@@ -139,12 +139,48 @@ app.use('/api/member-transfer-config', memberTransferConfigRoutes);
app.use('/api/training-groups', trainingGroupRoutes);
app.use('/api/training-times', trainingTimeRoutes);
app.use(express.static(path.join(__dirname, '../frontend/dist')));
// Middleware für dynamischen kanonischen Tag (vor express.static)
const setCanonicalTag = (req, res, next) => {
// Nur für HTML-Anfragen (nicht für API, Assets, etc.)
if (req.path.startsWith('/api') || req.path.match(/\.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|mp3|webmanifest|xml|txt)$/)) {
return next();
}
// Catch-All Handler für Frontend-Routen (muss nach den API-Routen stehen)
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../frontend/dist/index.html'));
});
// Prüfe, ob die Datei als statische Datei existiert (außer index.html)
const staticPath = path.join(__dirname, '../frontend/dist', req.path);
fs.access(staticPath, fs.constants.F_OK, (err) => {
if (!err && req.path !== '/' && req.path !== '/index.html') {
// Datei existiert und ist nicht index.html, lasse express.static sie servieren
return next();
}
// Datei existiert nicht oder ist index.html, serviere index.html mit dynamischem kanonischen Tag
const indexPath = path.join(__dirname, '../frontend/dist/index.html');
fs.readFile(indexPath, 'utf8', (err, data) => {
if (err) {
return next();
}
// Bestimme die kanonische URL (bevorzuge non-www)
const protocol = req.protocol || 'https';
const host = req.get('host') || 'tt-tagebuch.de';
const canonicalHost = host.replace(/^www\./, ''); // Entferne www falls vorhanden
const canonicalUrl = `${protocol}://${canonicalHost}${req.path === '/' ? '' : req.path}`;
// Ersetze den kanonischen Tag
const updatedData = data.replace(
/<link rel="canonical" href="[^"]*" \/>/,
`<link rel="canonical" href="${canonicalUrl}" />`
);
res.setHeader('Content-Type', 'text/html');
res.send(updatedData);
});
});
};
app.use(setCanonicalTag);
app.use(express.static(path.join(__dirname, '../frontend/dist')));
(async () => {
try {