Preventing trailing slash on domain name

2020-04-08 11:17发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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



回答4:

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:

  • Firefox (Windows, 6.0.2) = http://www.adobe.com/
  • Google Chrome (Windows, 13.0.782.220 m) = www.adobe.com
  • Opera (Windows 11.51) = www.adobe.com
  • Internet Explorer 9 = http://www.adobe.com/