Forum Moderators: open

Message Too Old, No Replies

Forward slashes (/) at the end of a URL

Is there a best practice on this subject?

         

rubenski

1:26 pm on Jul 20, 2004 (gmt 0)

10+ Year Member



Hi everyone,

I am building an index of some sort, based on a MYSQL db. I rewrite my URLs using Apache's mod_rewrite.

Right now www.mysite.nl/directory and www.mysite.nl/directory/ (<-- note the /) both show the same page.

Is there a 'best practice' with regard to the use or omission of the forward slash at the end of a URL?
If both URLs work, could that be seen as content duplication? It did take a while for Adsense ads to appear on the URL without the /, so perhaps the Mediapartners bot sees them as different pages?

Should I have both URLs display the same page or only the URL with or without the forward slash?

I see DMOZ and YAHOO automatically add a / if it is omitted. Is this default server behaviour or is it programmed?

Longhaired Genius

1:57 pm on Jul 20, 2004 (gmt 0)

10+ Year Member



I would add the slash.

www.mysite.nl/directory and www.mysite.nl/directory/ are different addresses. If Apache does not find a file "directory" it will look for a directory "directory" so more work for the server is involved.

The addition of the missing slash is both programed and default. In that Apache has to be set up to do it but I think that set up is in the default installation.

There's much more to it than this, spending some time with the Apache docs or a good Apache book is a worthwile thing to do to get to grips with the application's behaviour.

py9jmas

2:02 pm on Jul 20, 2004 (gmt 0)

10+ Year Member



Pick one convention and stick to it.
If you use both, I assume all your links are absolute? A relative link resolved with a base URL of www.mysite.nl/directory will probably take you somewhere different to the same link resolved with a base URL of www.mysite.nl/directory/.

I see DMOZ and YAHOO automatically add a / if it is omitted. Is this default server behaviour or is it programmed?

One would hope they are responding to 301 Moved permanently HTTP replies.

john_k

2:39 pm on Jul 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should add the slash if the resource is known to be a directory and not a file.

Omitting the "/" can cause a redirect at the server. Although the server can be configured to deal with this so that it doesn't send a redirect, it will involve more CPU cycles. Also, the HTML you write should be independent of the server implementation. So, one server might not do the redirect, but if the code moves to another server, it might have to do the redirect.

One exception to this is the root resource. That is, if you have a URL of "http://www.example.com" then the HTTP specifications dictate that the browser send the request just as if it had been written "http://www.example.com/". As others have said, for the root, you should pick one method or the other and stick to it.

Google and other search engines will attempt to equate the two anyway (for purposes of page rank), but better to control it yourself as much as possible.

rubenski

4:21 pm on Jul 20, 2004 (gmt 0)

10+ Year Member



It is pretty obvious I should go for the solution where mysite.nl/directory is forwarded to mysite.nl/directory/.

py9jmas are you suggesting a 301 moved permanent is best for this situation. If so, why?

Longhaired Genius. I'd love to read a book on Apache and I am planning to do so. If you have a suggestion for a good book I'd be happy to hear it as I am going on vacation on a week and I'll have plenty of time for reading then.

I have my URL rewriting for first level directories like this now:

RewriteRule ^([a-zA-Z]+)$ [mysite.nl...] [R=301,L]
RewriteRule ^(.*)/$ getcontent.php?cat=$1 [L]

The first rule catches "mysite.nl/directory" and sends it to "mysite.nl/directory/" and the second one catches "mysite.nl/directory/" and sends it to getcontent.php. Works like a charm. I hope you agree with this setup.

g1smd

10:07 pm on Jul 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Add the trailing slash to all links. It saves one step in the browser and server, but in text it also helps separate a URL from the surrounding text a little bit.