简体   繁体   English

如果同一目录下的文件名和文件夹名相同,如何从 URL 中删除 .html 扩展名

[英]How can you remove the .html extension from a URL if you have the same file name and folder name on the same directory

Ok, so I've tried looking up this answer, and talked to ppl on discord, but it doesn't seem like there is an answer for this, so this is probably my last stop.好的,所以我尝试查找此答案,并在 discord 上与 ppl 交谈,但似乎没有答案,所以这可能是我的最后一站。

This is my file structure这是我的文件结构

> 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)

The goal is: to remove the ".html" extension at the end of the page.目标是:删除页面末尾的“.html”扩展名。

Example:例子:
example.com/x instead of example.com/x.html; example.com/x而不是 example.com/x.html;
example.com/y/not instead of example.com/y/not.html** example.com/y/not代替 example.com/y/not.html**
Any ideas on how I can accomplish this?关于我如何做到这一点的任何想法?

I've used aa few different codes.我使用了一些不同的代码。 The one I'm currently using: (.htaccess)我目前正在使用的一个:(.htaccess)

RewriteEngine on
RewriteBase /

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

You can use DirectorySlash off in your htaccess file.您可以在 htaccess 文件中使用DirectorySlash off This will allow you to rewrite a file with the same name as directory.这将允许您重写与目录同名的文件。
At the top of your .htaccess ( before existing rules) put the following line:.htaccess的顶部(在现有规则之前)放置以下行:

DirectorySlash off

This turns off the slash on a directory and makes it possible to perform URL Rewriting.这会关闭目录上的斜线,并可以执行 URL 重写。 There are some advantages and disadvantages of using this.使用它有一些优点和缺点。

Advantages:优点:

You can use RewriteRule to remove the extension so you can just type /folder instead of /folder.html .您可以使用RewriteRule删除扩展名,因此您只需键入/folder而不是/folder.html

Disadvantages:缺点:

By disabling directorySlash functionalities your directory structure may be visible to browsers if they direct access your directory by going to /folder/ .通过禁用directorySlash功能,如果浏览器通过转到/folder/直接访问您的目录,则您的目录结构可能对浏览器可见。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除URL中的.html扩展名,当文件夹与.html文件同名时 - Remove the .html extension in the URL, when the folder has the same name as the .html file 如何维护文件夹和文件同名但隐藏文件扩展名的网站? - How can I maintain a website where a folder and file have the same name but also hide the extension of that file? 如何从.htaccess中的url中删除文件夹名称和文件扩展名? - How can I remove a folder name and file extension from url in .htaccess? htaccess 删除文件扩展名并允许同名文件夹 - htaccess remove file extension and allow folder of same name 如何具有相同名称的文件和目录 - How to Have a File and Directory With Same Name Apache:删除扩展名但文件和文件夹同名 - Apache: Drop extension but file and folder with same name 如何从url中删除.php扩展名,以命名为相同文件的文件,例如文件夹 - how to remove the .php extension from url, for a file named same like a folder 在node.js + express应用程序中,如何重定向URL以从先前站点中删除文件扩展名? - In an node.js + express app, how can you redirect a URL to remove a file extension from the previous site? 相同名称的文件和文件夹 - File and folder with same name .htaccess删除文件扩展名,强制尾随斜杠并允许文件夹与文件同名 - .htaccess Remove file extension, force trailing slash and allow folder with same name as file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM