Link to home
Start Free TrialLog in
Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on

Should your url's have a trailing slash?

I was running some SEO tests on various websites that tell you where you have problems and one of them said my URL didn't have a / at the end and that this was a problem e.g.: mysite.com/about-us/

If this is true, why?
ASKER CERTIFIED SOLUTION
Avatar of Dr. Klahn
Dr. Klahn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nicholas
Nicholas

Makes no difference, a trailing slash basically tells the browser I'm serving a default file (e.g. index.hml) but most cases this is not true and is just a routing action

The only way it can affect SEO is where you have cases where you use the url with and without the slash as it is seen as two different pages with the same content

So either use it for every url or don't use it at all
Avatar of Crazy Horse

ASKER

where you use the url with and without the slash

Do you mean the same url with and without the slash? Like:

mysite.com/about-us

mysite.com/about-us/

Can you actually even do that?
This is the error the site gives me when running a SEO check:

This website returns page content with or without a trailing slash on the URLs. Search engines might see these as separate pages with duplicate content which they could penalise for. Recommendation: Use a 301 redirect or a canonical meta tag to force search engines to see the correct version.
Yes, even tho they may be the same content they are regarded as two seperate pages

Re the next comment - that's what I'm saying above.
I don't really understand that. Is it saying that the problem is I can access the same page with or without the trailing slash and Google might see that as two separate pages with duplicate content? If that is the case, how do I fix it?
Add to your .htaccess

# To remove the trailing slash
RewriteRule ^(.*)/$ /$1 [R=301]

# To force a slash
RewriteRule ^(.*[^/])$ /$1/ [R=301]

Open in new window

Sorry if I am being thick as a plank, but am I meant to choose one of those or use both?
I just noticed if that I try add a slash at the end of my url I get this error, so they aren't the same thing it would seem i.e.: The url without a slash, and the one with a trailing slash.

INTERNAL SERVER ERROR

The server encountered an internal error or misconfiguration and was unable to complete your request.......
Your website is setup as to only recognize url's of a certain format, it obviously doesn't know what to do with the slash at the end (probably a routing thing)

So in .htaccess use
RewriteEngine on 
RewriteRule ^(.*)/$ /$1 [R=301]

Open in new window

I am not using wordpress or any CMS. I am using .php pages and in my .htaccess I have removed the .php so my urls are pretty.

What exactly will this do?

RewriteRule ^(.*)/$ /$1 [R=301]

Open in new window


Will it remove the Internal server error message?
It removes any slashes at the end
But isn't the idea that if someone types in the url with a trailing slash it should redirect to the version without a trailing slash instead of displaying an internal server error message?
Thats what the code above does
Ah, okay. I am going to only be able to try that tomorrow but will get back to you as soon as I have implemented the code and run the SEO checker test again to see if that error goes away.

So, just to confirm, Google won't now think I have duplicate pages because mysite.com/contact-us and mysite.com/contact-us/ go to exactly the same place?
Yep
mysite.com/contact-us/ will redirect to mysite.com/contact-us
Hmm.

I did that and when typing in:

https://mysite.com/contact-us/

It goes to url :

https://mysite.com/contact-us/.php/.php/.php

I already had this in my .htaccess which I think I put in to redirect to https instead of http

RewriteRule ^(.*)$ https://mysite.com/$1 [R=301,L]

Open in new window