Apache 2.2 重写 - 强制所有请求到 index.html

dae*_*aos 6 rewrite redirect apache-2.2

我需要强制所有请求,不管它们是什么 index.html。不幸的是,我尝试过的一切都没有正常工作。

以下在我看来应该可以工作,但没有。我的理解是,它表示任何不以 /index.html 结尾的内容都可以实际请求 index.html 文件。

重定向 302 !^/index.html http://www.example.com/index.html

任何帮助/澄清将不胜感激。

Pri*_*rix 16

在根文件夹上使用 .htaccess 这应该可以完成这项工作:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteRule . /index.html [R=302,L]
Run Code Online (Sandbox Code Playgroud)

条件和规则是,如果请求与 index.html 不匹配,它将重定向到它


Nad*_*dir 8

@prix 的回答很好。我自己需要添加条件,以便正常资源(图像和 css)正常提供:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt|svg|woff|ttf|eot)$
RewriteRule . index.html [L]
Run Code Online (Sandbox Code Playgroud)