You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular v17 SSR+SSG页面刷新触发301重定向问题求助

Angular v17 SSR+SSG 预渲染页面刷新触发301重定向问题

在Angular v17 SSR+SSG应用中,点击routerLink跳转页面时服务器正常加载.js模块展示内容,但刷新已预渲染的页面(如url/cases)时会触发301重定向到url/cases/,重定向后返回200状态码。需修复该问题以提升SEO效果。

现有配置

.htaccess 配置

<IfModule Litespeed>
    RewriteEngine On

    # Redirect www to non-www and HTTP to HTTPS
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

    # Existing rules for handling files and serving index.html
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*) /index.html [NC,L]
</IfModule>

server.ts 配置

import 'zone.js/node';

import { APP_BASE_HREF } from '@angular/common';
import { CommonEngine } from '@angular/ssr';
import * as express from 'express';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import AppServerModule from './src/main.server';

// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
  const server = express();
  const distFolder = join(process.cwd(), '../browser');
  const indexHtml = existsSync(join(distFolder, 'index.original.html'))
    ? join(distFolder, 'index.original.html')
    : join(distFolder, 'index.html');

  const commonEngine = new CommonEngine();

  server.set('view engine', 'html');
  server.set('views', distFolder);

  // Example Express Rest API endpoints
  // server.get('/api/**', (req, res) => { });
  // Serve static files from /browser
  server.get(
    '*.*',
    express.static(distFolder, {
      maxAge: '1y',
      redirect: false
    })
  );

  // All regular routes use the Angular engine
  server.get('*', (req, res, next) => {
    const { protocol, originalUrl, baseUrl, headers } = req;

    const requestedPath = `${distFolder}${originalUrl}`;

    // Check if the requested path corresponds to an existing file
    if (existsSync(requestedPath)) {
      // Serve the file directly without rendering Angular
      return res.sendFile(requestedPath);
    }

    commonEngine
      .render({
        bootstrap: AppServerModule,
        documentFilePath: indexHtml,
        url: `${protocol}://${headers.host}${originalUrl}`,
        publicPath: distFolder,
        providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
      })
      .then((html) => res.send(html))
      .catch((err) => next(err));
  });

  return server;
}

function isRunningOnApachePassenger(): boolean {
  return moduleFilename.includes('lsnode.js');
}

function run(): void {
  // Start up the Node server
  const server = app();

  if (isRunningOnApachePassenger()) {
    server.listen(() => {
      console.log('Node Express listening to Passenger Apache');
    });
    return;
  }

  const port = process.env['PORT'] || 4000;

  server.listen(port, () => {
    console.log(`Node Express server listening on http://localhost:${port}`);
  });
}

// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = (mainModule && mainModule.filename) || '';
if (
  moduleFilename === __filename ||
  moduleFilename.includes('iisnode') ||
  isRunningOnApachePassenger()
) {
  run();
}

export default AppServerModule;

路由配置

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  {
    path: '',
    loadChildren: () =>
      import('./pages/home/home.module').then((m) => m.HomeModule),
  },
  {
    path: 'over-ons',
    loadChildren: () =>
      import('./pages/over-ons/over-ons.module').then((m) => m.OverOnsModule),
  },
  {
    path: 'contact',
    loadChildren: () =>
      import('./pages/contact/contact.module').then((m) => m.ContactModule),
  },
  {
    path: 'expertises',
    loadChildren: () =>
      import('./pages/expertises/expertises.module').then((m) => m.ExpertisesModule),
  },
  {
    path: 'expertises/:slug',
    loadChildren: () =>
      import(
        './pages/expertises/expertise-detail/expertise-detail.module'
      ).then((m) => m.ExpertiseDetailModule),
  },
  {
    path: 'cases',
    loadChildren: () =>
      import('./pages/cases/cases.module').then((m) => m.CasesModule),
  },
  {
    path: 'cases/:slug',
    loadChildren: () =>
      import('./pages/cases/case-detail/case-detail.module').then(
        (m) => m.CaseDetailModule
      ),
  },
  {
    path: 'blogs',
    loadChildren: () =>
      import('./pages/blogs/blogs.module').then((m) => m.BlogsModule),
  },
  {
    path: 'blogs/:slug',
    loadChildren: () =>
      import('./pages/blogs/blog-detail/blog-detail.module').then(
        (m) => m.BlogDetailModule
      ),
  },
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, {
      scrollPositionRestoration: 'enabled',
      anchorScrolling: 'enabled',
      scrollOffset: [0, 150]
    }),
  ],
  exports: [RouterModule],
})
export class AppRoutingModule {}

已尝试的解决方案

  • 修改server.ts中express.static配置添加redirect: false
  • 调整路由配置,为路由末尾添加斜杠
  • 修改.htaccess规则

以上方法均未解决问题,寻求可行的修复方案。

内容的提问来源于stack exchange,提问作者M. Yilmaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 03:12:43