Trailing Slash – Creating Correct Link Structure

When building your web site it’s important to incorporate the correct link structure from the beginning. This will save a lot of headaches in the future should you decide to expand your web site.

1. Should you link to your home page with or without a trailing slash?

http://www.example.com (without the trailing slash)

http://www.example.com (with the trailing slash)

Answer

Put the index file in the root of the domain, and call it index.html.

On all of your pages where you link to the index file, link to it using just / or http://www.domain.com/ and it will work fine.

If you link to index.html then if you ever change to index.php then all of your links will immediately be broken. Omit the filename
from the links!

What if your current site’s home page has no trailing slash?

Answer

If you need to redirect http://mysite.com to http://www.mysite.com/ and you’ve got mod_rewrite enabled on your server you can put this in your .htaccess file:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]

Some search engines (ie Yahoo) remove the trailing slash for some urls that end with a slash – you can force them by creating a
.htaccess file.

For a more in-depth explanation read:

How to Redirect a Web Page Using a 301 Redirect

Comments

  1. Thanks for the post. However, I think your rewrite rule will only add a trailing slash to the parent URL and not the directories.

    Please correct me if I’m wrong, but wouldn’t you want to add a trailing slash to the end of this line?

    RewriteRule ^(.*)$ http://www.example.com/$1/

    That way example.com/directory/sub-directory gets a trailing slash at the end: example.com/directory/sub-directory/

  2. Sick! Just received a brand-new Pearl and I can now read your weblog on my phone’s browser, it didn’t work on my aged 1.

Trackbacks

  1. […] Trailing Slash – Creating Correct Link Structure […]

Speak Your Mind

*