【问题标题】:Cannot hide all extensions using htaccess无法使用 htaccess 隐藏所有扩展
【发布时间】:2014-09-23 23:46:00
【问题描述】:

我无法让我的 htaccess 文件隐藏所有文件扩展名(在我的情况下为 html 和 php)。

如果我输入“example.com/home.html”,

我希望在地址栏中显示“example.com/home”。

这是否可以在不将我的 html 代码更改为 'href="/home" ' 的情况下使用 htaccess 来实现?

提前致谢。

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    试试:

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} \ /+(.+)\.html(\?\ )
    RewriteRule ^ /%1 [L,R=301]
    
    RewriteCond %{DOCUMENT_ROOT}/$1.html -f
    RewriteRule ^(.*)$ /$1.html [L]
    

    在文档根目录的 htaccess 文件中。

    【讨论】:

    • 不好。结果与我发现的其他代码相同。它允许文件 tp 在没有扩展名的情况下运行,但仍显示在地址栏中。
    【解决方案2】:

    搜索了一些,发现您已经在另一篇文章中提供了解决方案。

    # browser requests html
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.html
    RewriteRule ^/?(.*)\.html$ /$1 [L,R=301]
    
    # check to see if the request is for a html file:
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^/?(.*)$ /$1.html [L]
    

    【讨论】:

    • 这个解决方案是否也适用于隐藏.php 扩展?
    • 只是 html。我想如果您将代码中的所有 html 替换为您想要隐藏的扩展名,它将起作用。
    猜你喜欢
    • 2017-01-29
    • 1970-01-01
    • 2017-09-24
    • 2013-08-13
    • 2012-04-19
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 2014-01-18
    相关资源
    最近更新 更多