[英]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.