防止域名尾随斜杠
我希望我的网站显示为 www.mysite.com,而不是 www.mysite.com/
Apache 默认情况下是否在域名后面添加尾部斜杠,或者浏览器附加它?如果我想使用 .htaccess 来防止这种情况,那么 url 重写规则是什么?
I want my site to show up as www.mysite.com, not www.mysite.com/
Does Apache add a trailing slash after a domain name by default, or does the browser append it? If I want to prevent this using an .htaccess, what would the url rewrite rule be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您请求:
http://myhost.com
该请求在 HTTP 中需要如下所示:
GET / HTTP/1.0
主机:myhost.com
由于历史原因,某些浏览器确实附加了斜杠,因为否则它会转换为
GETHTTP/1.0
主机:myhost.com
这将是非法请求。
请注意:
http://myhost.com/page
是合法的,因为它转换为:
GET /page HTTP/1.0
主机:myhost.com
If you request:
http://myhost.com
The request needs to look like this in HTTP:
GET / HTTP/1.0
Host: myhost.com
For historical reasons, some browsers did append the slash because otherwise it translates to
GET <nothing> HTTP/1.0
Host: myhost.com
Which would be an illegal request.
Note that:
http://myhost.com/page
is legal, because it translates to:
GET /page HTTP/1.0
Host: myhost.com
浏览器在请求 URL 时会自动添加这样的斜杠。 如何在地址栏中显示它是一个不同的故事。
例如:
www.adobe.com
- 在不同的浏览器中键入它,看看如何他们将显示它:http://www.adobe.com/
www.adobe.com歌剧
www.adobe.com
http://www.adobe.com/
Browser adds such slash automatically when requesting the URL. How it displaying in address bar it's a different story.
For example:
www.adobe.com
-- type it in different browsers and see how they will display it:http://www.adobe.com/
www.adobe.com
www.adobe.com
http://www.adobe.com/
http:// www.searchenginejournal.com/linking-issues-why-a-trailing-slash-in-the-url-does-matter/13021/
http://www.alistapart.com/articles/slashforward/
URL 最初用于对目录进行建模,所以需要尾部斜杠。我认为如果您没有尾部斜杠,某些网络服务器将无法正确找到内容。
http://www.searchenginejournal.com/linking-issues-why-a-trailing-slash-in-the-url-does-matter/13021/
http://www.alistapart.com/articles/slashforward/
URLs were initially used to model directories, so the trailing slash was required. I think if you don't have the trailing slash some webservers will not be able to find the content correctly.
正如安东尼的第一个链接所解释的,斜线是地址的一部分。每个域(不仅仅是“绝大多数”)都有一个类似于 www.mysite.com 的名称,但这只是一个域名,而不是 URL。 URL是一个文件的地址,即协议+域名+路径文件,所以 http://www.mysite.com/< /a> 由 DirectoryIndex 添加了缺失的文件名,因此是一个 URL,但是 http://www.mysite.com 只是没有任何意义,因为在这种情况下文件路径将是 空的。
您的浏览器不显示 URL 中无聊的部分这一事实与您网站的配置无关。
如果确实相同的浏览器在不同的网站上表现不同,我很想知道您使用的浏览器和网站。
As explained by Anthony's first link, the slash is part of the address. Every domain (and not just "the vast majority") has a name resembling www.mysite.com, but this is just a domain name, not an URL. An URL is the address of a file, ie protocol+domainname+pathfile, so http://www.mysite.com/ is added the missing filename by DirectoryIndex and therefore is an URL, but http://www.mysite.com just doesn't mean anything since in this case the file path would be empty.
The fact that your browser doesn't display the boring parts of your URL is not related to your website's configuration.
If really the same browser behaves differently on different websites, I would be curious to know what browser and what websites you used.