如何链接到没有 .html 扩展名的页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16919526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:03:14  来源:igfitidea点击:

How to link to pages without the .html extension?

html

提问by user2451987

I would like to link to pages inside my website, e.g: Not: mywebsite.com/about.html But: mywebsite.com/about/

我想链接到我网站内的页面,例如:不是:mywebsite.com/about.html 但是:mywebsite.com/about/

I've seen various websites doing this but it looks like they also react differently to things:

我见过各种网站这样做,但看起来他们对事物的反应也不同:

Apple.com: apple.com/iphone/ works, apple.com/iphone/index.html works, apple.com/iphone redirects.

Apple.com:apple.com/iphone/ 作品,apple.com/iphone/index.html 作品,apple.com/iphone 重定向。

Opera.com: opera.com/mobile/ redirects, opera.com/mobile works, opera.com/mobile.html does notwork.

Opera.com:opera.com/mobile/ 重定向,opera.com/mobile 有效,opera.com/mobile.html无效

Mozilla.com: mozilla.org/en-US/ works, mozilla.org/en-US redirects, mozilla.org/en-US/index.html does notwork.

Mozilla.com:mozilla.org/en-US/ 有效,mozilla.org/en-US 重定向,mozilla.org/en-US/index.html无效

Which leads to another question: Are there different methods for this?

这就引出了另一个问题:对此有不同的方法吗?

Edit:It seems that Apple uses a folder for every page, e.g. a folder called 'iphone' with an index.html file inside it? But Opera and Mozilla use something in the .htaccess file?

编辑:Apple 似乎为每个页面使用一个文件夹,例如一个名为“iphone”的文件夹,里面有一个 index.html 文件?但是 Opera 和 Mozilla 在 .htaccess 文件中使用了一些东西?

回答by Mudassar Arshad

Removing Extensions

删除扩展

To remove the .php extension from a PHP file for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:

要从 PHP 文件中删除 .php 扩展名,例如 yoursite.com/wallpaper.php 到 yoursite.com/wallpaper,您必须在 .htaccess 文件中添加以下代码:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ .php [NC,L]

If you want to remove the .html extension from a html file for example yoursite.com/wallpaper.html to yoursite.com/wallpaper you simply have to alter the last line from the code above to match the filename:

如果您想从 html 文件中删除 .html 扩展名,例如 yoursite.com/wallpaper.html 到 yoursite.com/wallpaper,您只需更改上面代码的最后一行以匹配文件名:

RewriteRule ^([^\.]+)$ .html [NC,L]

That's it! You can now link pages inside the HTML document without needing to add the extension of the page. For example:

就是这样!您现在可以链接 HTML 文档内的页面,而无需添加页面扩展名。例如:

 <a href="http://whatever.com/wallpaper" title="wallpaper">wallpaper</a>

回答by ibi0tux

They are using .htaccessand URL rewriting. This is part of server configuration. You can not do it with html only.

他们正在使用.htaccessURL rewriting。这是服务器配置的一部分。你不能只用 html 来做。

This pageexplains basics of URL rewriting.

解释URL重写的基础。

回答by ibi0tux

You folder then has to contain a file: index.*. Like: /iphone/index.html, which can be /iphone/ as well

您的文件夹必须包含一个文件:index.*. 比如:/iphone/index.html,也可以是/iphone/

Or work with .htaccess

或与 .htaccess

回答by Genus Amar

In the .htaccess file in your sites root folder just add the following line:

在站点根文件夹中的 .htaccess 文件中,只需添加以下行:

# ---- Render pages without urls
Options +MultiViews

回答by Kal

The most upvoted answerdoesn't check whether the URL points to a directory, so you're going to get some mysterious 'not found' errors when it tries to append '.html' to a directory path. Easily fixed:

最受好评的答案不检查 URL 是否指向目录,因此当它尝试将“.html”附加到目录路径时,您会遇到一些神秘的“未找到”错误。轻松修复:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ .html [L]

The first condition will only pass if the path does notpoint to a valid directory. The second will only pass if the path points to a valid file when the .html extension is added. If bothconditions pass, the rewrite rule simply adds ‘.html' to the filename.

第一个条件只有在路径指向有效目录时才会通过。仅当添加 .html 扩展名时路径指向有效文件时,第二个才会通过。如果两个条件都通过,则重写规则只是将“.html”添加到文件名中。

Notice that we can just match the entire path with .*. You can reject paths that contain a period character if you wish, but it's not really necessary since you've already checked that {REQUEST_FILENAME}.htmlis a valid file. In any case, it is unnecessary to escape a period character when it's inside a character class. I know you see this [^\.]everywhere, but the slash is redundant. [^.]is how to write it and look like a regex pro.

请注意,我们可以将整个路径与.*. 如果您愿意,您可以拒绝包含句点字符的路径,但这并不是真正必要的,因为您已经检查过它{REQUEST_FILENAME}.html是一个有效文件。在任何情况下,当句点字符位于字符类中时,都没有必要对其进行转义。我知道你[^\.]到处都能看到这个,但斜线是多余的。[^.]是如何编写它并且看起来像一个正则表达式专家。

This kind of redirect will be invisible to the user because, by default, mod_rewrite does the substitution internally, without informing the browser. If you wanted to do a permanent redirect, you would add the [R=301] flag at the end.

这种重定向对用户是不可见的,因为默认情况下,mod_rewrite 在内部进行替换,而不通知浏览器。如果您想进行永久重定向,您可以在末尾添加 [R=301] 标志。

回答by harold ramos

Make your href attribute equal to the page you want to link or .. If you need to move up a directory.

使您的 href 属性等于您要链接的页面或 .. 如果您需要向上移动一个目录。

Ex: href="contact.html" Ex: href="../links/contact.html"

例如:href="contact.html" 例如:href="../links/contact.html"