Detect trailing slash after domain?

684 views
Skip to first unread message

Erik Unger

unread,
Jul 29, 2012, 6:00:10 AM7/29/12
to golan...@googlegroups.com
Is there a way in net/http to distinguish between requests to http://example.com and http://example.com/ ?

I'd like to add a redirect to the trailing slash version for SEO purposes.

-Erik

DisposaBoy

unread,
Jul 29, 2012, 6:43:45 AM7/29/12
to golan...@googlegroups.com
Are you sure the http pkg doesn't already do that? 

Andrew Gerrand

unread,
Jul 29, 2012, 5:23:10 PM7/29/12
to Erik Unger, golan...@googlegroups.com
How can a web browser make a request to http://example.com ?

When an HTTP client connects, doesn't it need to say "GET /", at bare minimum?

Andrew

jason

unread,
Jul 29, 2012, 6:20:52 PM7/29/12
to golan...@googlegroups.com
This is probably something you want to detect and rewrite in your proxy level.  For example if you're using nginx or Apache this, is something you'd do as a rewrite permanent rule.

Though each browser will treat the trailing slash differently. FF likes to add them where Chrome sends what ever the user types.  For example. going to reddit.com/ will be requested as that, but once loaded will displayed as reddit.com, and any reloading of the page after that will use just reddit.com without the trailing slash. So you might find your server sending a 30(2|4) every time the user reloads your page

Now a trailing slash on the end of an actual path, (not root), can be a bit trickier, since they have two different meanings in the http handle func object.  With no slash meaning endpoint, and with slash everything under that path being sent to the handler func.

Patrick Mylund Nielsen

unread,
Jul 29, 2012, 11:32:34 PM7/29/12
to jason, golan...@googlegroups.com
You could make "/" a handler that either responds to path "/", or if the path is something else, checks if strings.HasSuffix(req.URL.Path, "/"), and if not, http.Redirect(w, req, req.URL.Path + "/", http.StatusMovedPermanently), or if it does, do http.Error(w, "Page not found", http.StatusNotFound)

Patrick Mylund Nielsen

unread,
Jul 30, 2012, 8:31:25 AM7/30/12
to ma...@ungerik.net, golan...@googlegroups.com, Erik Unger
But this is for other URIs than "/", right, e.g. /foo rather than /foo/? If you want to fix that, you can do HTTP 301 redirects or use rel canonical. If only Google rankings are important, the latter is fine. If not, the former, since other robots might not care about rel canonical.

On Mon, Jul 30, 2012 at 12:00 AM, <ma...@ungerik.net> wrote:
On Sunday, July 29, 2012 11:23:10 PM UTC+2, Andrew Gerrand wrote:
When an HTTP client connects, doesn't it need to say "GET /", at bare minimum?

I don't know how it works on a HTTP level and if it's possible at all, but SEO tools are complaining about duplicate content from two different URLs (which doesn't mean that Google is also seeing that way).

One solution:
<link rel='canonical' href='http://example.com/'>

-Erik

Reply all
Reply to author
Forward
0 new messages