.htaccess - 创建没有 .html 结尾形式的 404 页面

.htaccess - create 404 page without .html ending form

我想在我的网站上创建一个默认的 404 页面,我知道我该怎么做:

ErrorDocument 404 http://example.com/404.html

但是如何从 url 创建到此页面的特殊重定向:“http://example.com/page-not-found” ??我在 Google 和此处进行了搜索,但一无所获。

所以最后,当用户访问页面时:http://example.com/test-page 而这个 url 在我的网站上不存在,脚本应该将用户重定向到页面:“http://example.com/page-not-found”,这个 url 应该显示 404.html 文件中的 html 代码。

谢谢。

这可以通过 .htaccess 和 mod_rewrite 来完成。

修改:

ErrorDocument 404 http://example.com/page-notfound.html

然后在你的 .htaccess 添加:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ .html [NC,L]

当然,前提是您启用了 mod_rewrite。

在您的根目录中尝试这样,

RewriteEngine on

RewriteBase /my_catalog/

# rewritten rule
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ .html [L]

# not found rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ Page-not-found [R=301,L]

Page-not-found 是你所谓的错误 html 文件如果传入的请求既不是文件也不是你服务器中的目录它将提供页面未找到的内容,它在内部重写为 page-not-found.html.