从 url 中删除 index.html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22481028/
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
Removing the index.html from url
提问by David Hakkert
Ok, maybe a pretty dumb question but I can't find an answer on Google.
好吧,也许是一个非常愚蠢的问题,但我在谷歌上找不到答案。
I am coding my site by hand. How do I make the index.html disappear from the url's? Do I use a piece of code in my html? Do I have to change my href's in my files?
我正在手动编码我的网站。如何使 index.html 从 url 中消失?我在我的 html 中使用了一段代码吗?我必须在我的文件中更改我的 href 吗?
Hope you guys can help!
希望大家帮帮忙!
EDIT: I've tried this with a .htaccess file
编辑:我用 .htaccess 文件试过这个
RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
It does work, but all my links aren't working anymore. I discovered I had to remove all the index.html from the href's in my documents. But that would be a lot of work. Is there an other code for .htaccess that just hides index.html?
它确实有效,但我所有的链接都不再有效。我发现我必须从文档中的 href 中删除所有 index.html。但这将是很多工作。.htaccess 是否还有其他代码只是隐藏了 index.html?
回答by Darko Atanasov
A SIMPLE WAY TO DO THIS in Html:
在 Html 中执行此操作的简单方法:
(example in my case is a simple dual language site)
(在我的例子中是一个简单的双语言网站)
If your link looks like this:
如果您的链接如下所示:
<a href="index.html">Homepage</a>
You should change it to this:
你应该把它改成这样:
<a href="/">Homepage</a>
If trying to link to another folder in your directory, like is my example:
如果尝试链接到目录中的另一个文件夹,就像我的示例:
<a href="en/index.html">English language</a>
You should change it to this:
你应该把它改成这样:
<a href="en">English language</a>
Notice that "/" goes back to your root directory and automatically selects index.html, so that is why I used "en" for the English language site, because the name of the folder in that case is "en". You should also make sure that you have index.html in your English language folder, and not index-en.html.
请注意,“/”会返回到您的根目录并自动选择 index.html,这就是我为英文站点使用“en”的原因,因为在这种情况下文件夹的名称是“en”。您还应该确保您的英语语言文件夹中有 index.html,而不是 index-en.html。
回答by vogomatix
Apache has .htaccess files and mod_rewrite, In your .htaccess file, set:
Apache 有 .htaccess 文件和 mod_rewrite,在你的 .htaccess 文件中,设置:
DirectoryIndex index.html
You can also set this up in the Apache site config filestoo
您也可以在Apache设置此网站的配置文件太
You can specify a list of filenames, so if it doesn't find the first it moves to the next.
你可以指定一个文件名列表,所以如果它没有找到第一个,它就会移动到下一个。
IIS has .config files
IIS 有.config 文件
回答by cnst
Great SEO idea! This is similar to nginx redirect loop, remove index.php from urland Apache .htaccess to hide both .php and .html extentions, as well as mod_rewrite not working htaccess— the idea here, for both Apache's mod_rewrite and nginx ngx_http_rewrite, depends on the differences between the externaland internalURLs — in order for the redirect from /index.html
to /
work, yet not cause redirect loopswhen actually serving index.html
, you gotta make sure you only issue the redirect for externalindex.html
requests, but never for internalones; this can only be accomplished by looking into the actual request_uri
.
伟大的 SEO 想法!这类似于nginx 重定向循环,从 url和Apache .htaccess 中删除 index.php以隐藏 .php 和 .html 扩展名,以及mod_rewrite 不起作用 htaccess— 这里的想法,对于 Apache 的 mod_rewrite 和 nginx ngx_http_rewrite,取决于该之间的差异外部和内部的URL -为了从重定向/index.html
到/
工作中,尚未引起重定向循环实际投放时index.html
,你必须确保你只发出重定向外部index.html
的请求,但从来没有对内部的人; 这只能通过查看实际request_uri
.
Here's the code for nginx ngx_mod_rewrite:
这是 nginx ngx_mod_rewrite 的代码:
index index.html index.txt;
if ($request_uri ~ "^(.*/)index\.(html|txt)$") { return 301 ; }
On Apache's mod_rewrite, it'll be something like the following:
在 Apache 的 mod_rewrite 上,它将类似于以下内容:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^.*/index\.html$
RewriteRule ^(.*/)index.html$ [R,L]
Related:
有关的:
- nginx redirect loop, remove index.php from url
- Apache .htaccess to hide both .php and .html extentions
- mod_rewrite not working htaccess
References:
参考:
回答by murtuza hussain
mod_rewrite module is responsible for all the rewriteEngine. Check on your server whether is module is present and enable.
mod_rewrite 模块负责所有的 rewriteEngine。检查您的服务器是否存在模块并启用。
回答by Joe Harper
I think this is configured in IIS when you deploy the site, I'm not to sure on it but I'm sure you can specify a start point that your URL will use when you just enter the UL, that implies the Index.html page.
我认为这是在您部署站点时在 IIS 中配置的,我不确定,但我确定您可以指定一个起点,当您输入 UL 时,您的 URL 将使用该起点,这意味着 Index.html页。
Sorry I'm not too helpful here, hopefully it will point you in the right direction.
抱歉,我在这里没有太大帮助,希望它能为您指明正确的方向。
Often these things such as Apache or IIS have this set up already, and it looks for the Index.html
, Index.php
first when you just put your URL in.
通常,这些东西,如Apache或IIS有这个设置已经和它看起来的Index.html
,Index.php
第一,当你只是把你的网址英寸
回答by AlexanderFT
Change the link that goes to your homepage to the website address. You may have:
将进入您主页的链接更改为网站地址。你可能有:
<a href="index.html">Link Here</a>
Change that to:
将其更改为:
<a href="http://www.domain.com">Link</a>
回答by Ashish Yadav
You need to create a file called '.htaccess' in the root directory of your site containing the following code:
您需要在站点的根目录中创建一个名为“.htaccess”的文件,其中包含以下代码:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ .html [NC,L]
And then make sure all the links on your site don't contain '.html' at the end, e.g.:
然后确保您网站上的所有链接最后都不包含“.html”,例如:
<a href="/Home.html">
should be replaced with:
应替换为:
<a href="/Home">
Hope this helps!
希望这可以帮助!
if you dont find .htaccess, you just need to create a new file using your text editor the same way you would any other html or css file, but save it as simply '.htaccess'
如果您没有找到 .htaccess,您只需要使用文本编辑器创建一个新文件,就像创建任何其他 html 或 css 文件一样,但将其保存为简单的“.htaccess”
And save it into the root directory, so the same folder that you have your index.html file.
并将其保存到根目录中,因此与您拥有 index.html 文件的文件夹相同。
回答by Steve Martin
Simply don't type the index.html in your browser and don't advertise it as such.
简单地不要在浏览器中输入 index.html 并且不要这样宣传它。
You can set the 'default document' on the web server (whichever you happen to be using) to serve 'index.html' in the absence of a file part. This way, someone going to http://www.mysite.comwould be served http://www.mysite.com/index.html
您可以在 web 服务器上设置“默认文档”(无论您使用的是哪个),以便在没有文件部分的情况下提供“index.html”。这样,某人去http://www.mysite.com将得到http://www.mysite.com/index.html