SvelteKit 项目静态构建的 Apache2 配置

问题描述 投票:0回答:1

我有一个 SvelteKit 项目,其中有两个页面,结构如下:

+layout.js
+layout.svelte
+page.svelte
page-two
       |__+page.svelte

第二页只能通过输入 URL 访问,因为根页面没有链接到它。当使用

vite dev
在本地运行项目时,将
\page-two
附加到 URL 会正确加载页面,但是当将项目部署到尝试访问
page-two
的 Apache2 服务器时,返回 HTTP 404。

以下是我的Apache2配置:

        ServerAdmin [email protected]
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/project/build/

        // Logging
        // SSL

从 URL 访问第二页我缺少什么配置? 我尝试在this gist中添加规则但无济于事。

apache apache2 svelte sveltekit
1个回答
1
投票

你有选择:

A. 启用 SvelteKit 的 trailingSlash 和 Apache 的 DirectoryIndex

// src/routes/+layout.js
export const trailingSlash = 'always';

并更新内部链接以包含尾部斜杠。

B. 创建一个重写规则,在请求为

page-two.html
 时查找 
/page-two

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
© www.soinside.com 2019 - 2024. All rights reserved.