【问题标题】:Removing url file extension in .htaccess删除 .htaccess 中的 url 文件扩展名
【发布时间】:2014-01-06 18:32:45
【问题描述】:

我知道这是一项非常常见的任务,并且已经在这里讨论了很多,但我仍然无法让它发挥作用。在做了一些研究后,我将此代码添加到了我的 .htaccess 文件中。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

但它仍然不起作用并在我的页面上显示 .html。 有什么问题?

谢谢

【问题讨论】:

    标签: regex apache .htaccess url mod-rewrite


    【解决方案1】:

    您需要一个额外的规则来将foo.html 外部重定向到foo。让你的 .htaccess 像这样:

    RewriteEngine on
    
    RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
    RewriteRule ^ /%1? [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.html -f
    RewriteRule ^(.+?)/?$ /$1.html [L]
    

    【讨论】:

    • 嗯,还是不行。但这很奇怪。当我在根目录中有 .htaccess 时,我的页面像 /index.html 一样加载 - 当我删除 .html 时,它给了我这个 404 错误:在此找不到请求的 URL /web/index-en.html服务器。但是当我完全删除 .htaccess 时,它仍然以 index.html 格式加载,但是当我删除 .html 时,网站也加载得很好。
    • 查看更新的代码,确保它位于您网站的根目录中。
    • 我发现另一个错字并修正了它。你能再试试更新的代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多