html - 如何假设网站 URL 末尾的 .html?

标签 html regex apache .htaccess mod-rewrite

我目前有一个网站,其中有 www.website.com/about.html

但是,如果我在 url 中键入 www.website.com/about/,它会显示默认的 error 404 页面。

我在某处看到过有关编辑 htaccess 文件的内容,但我不确定该怎么做。

最佳答案

只需在您的 htaccess 顶部添加此行即可减少您的 URL 扩展名:

Options +MultiViews

或者,如果您更喜欢 mod_rewrite,则在您的 DOCUMENT_ROOT/.htaccess 文件中使用此代码:

RewriteEngine On

# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

# To internally forward /dir/file to /dir/file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]

关于html - 如何假设网站 URL 末尾的 .html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21522380/

相关文章:

javascript - 在 html5 中 dragleave 后下降

regex - bash - 提取字符串的一部分

php - 如何解决 PHP lookbehind 固定宽度限制?

php - 在 Wordpress 上启用漂亮的永久链接 - apache 配置不起作用

javascript - 可点击的 HTML 下拉菜单

javascript - 将 javascript 代码内的文本 attr 更改为 "readonly false"

html - 使用@fontface,如何将不同的样式应用于不同的字体系列?

Javascript 正则表达式替换,多行

php - header ("location") 导致 [500] 内部服务器错误?

php - Nginx 配置权限和所有权