【问题标题】:How can you remove the .html extension from a URL if you have the same file name and folder name on the same directory如果您在同一目录中具有相同的文件名和文件夹名,如何从 URL 中删除 .html 扩展名
【发布时间】:2021-03-10 02:56:56
【问题描述】:

好的,所以我已经尝试查找此答案,并在 discord 上与 ppl 进行了交谈,但似乎没有答案,所以这可能是我的最后一站。

这是我的文件结构

> index.html
> x (FOLDER)
   > i_am_in_x.html
   > xD.html
> y (FOLDER)
   > not.html
   > yes.html
> somefile.html
> x.html
> y.html
> styles (FOLDER)
> images (FOLDER)

目标是:删除页面末尾的“.html”扩展名。

例子:
example.com/x 而不是 example.com/x.html;
example.com/y/not 而不是 example.com/y/not.html**
关于如何实现这一点的任何想法?

我使用了几个不同的代码。我目前正在使用的那个:(.htaccess)

RewriteEngine on
RewriteBase /

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

【问题讨论】:

  • 请在您现在使用的问题中添加您的 htaccess 文件,谢谢。
  • 我曾经用过几个不同的。我在问题中添加了我现在正在使用的那个。

标签: .htaccess url


【解决方案1】:

您可以在 htaccess 文件中使用DirectorySlash off。这将允许您重写与目录同名的文件。
.htaccess 的顶部(在现有规则之前)放置以下行:

DirectorySlash off

这会关闭目录上的斜线,并可以执行 URL 重写。使用它有一些优点和缺点。

优点:

您可以使用RewriteRule 删除扩展名,这样您就可以输入/folder 而不是/folder.html

缺点:

通过禁用directorySlash 功能,如果浏览器通过转到/folder/ 直接访问您的目录,则您的目录结构可能对浏览器可见。

参考https://httpd.apache.org/docs/2.4/mod/mod_dir.html#directoryslash

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    相关资源
    最近更新 更多