Removing Trailing Slashes from URLs

Discuss Search Engine Optimization in relation to Joomla! 2.5. This forum will also have discussions on SEF/SEO Joomla! 2.5 extensions.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Locked
gsadeghi
Joomla! Intern
Joomla! Intern
Posts: 64
Joined: Sun Aug 28, 2011 9:20 pm

Removing Trailing Slashes from URLs

Post by gsadeghi » Wed Mar 07, 2012 3:44 pm

Hi guys,

I've been working on optimizing the my website built with Joomla. I need to remove trailing slashes from my urls since the search engines have been indexing them as two separate ones.
example:
www.mywebsite.com/link/
www.mywebsite.com/link

Right now i have a code on .htaccess which redirects the non-www version of my website to www version. I need to either remove or add slashes to all my urls to make sure they stay consistence.

Could anybody help me with this?

Thanks

lowrykun
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 107
Joined: Wed Jul 20, 2011 7:02 pm

Re: Removing Trailing Slashes from URLs

Post by lowrykun » Mon Apr 30, 2012 9:09 pm

using apache mod_rewrite in your .htaccess file...

RewriteBase /
RewriteRule ^(.*)/$ $1 [R,L]

gsadeghi
Joomla! Intern
Joomla! Intern
Posts: 64
Joined: Sun Aug 28, 2011 9:20 pm

Re: Removing Trailing Slashes from URLs

Post by gsadeghi » Mon Apr 30, 2012 9:15 pm

lowrykun wrote:using apache mod_rewrite in your .htaccess file...

RewriteBase /
RewriteRule ^(.*)/$ $1 [R,L]
Thanks for your reply. I'm glad you brought this subject up so I can update this thread. Actually i solve the issue using the code below

RewriteCond %{HTTP_HOST} ^(www.)?mywebsite\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

the problem was I was inserting the code in the wrong spot on .htaccess file. I put the code between the area for custom redirects and it worked well.

lowrykun
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 107
Joined: Wed Jul 20, 2011 7:02 pm

Re: Removing Trailing Slashes from URLs

Post by lowrykun » Mon Apr 30, 2012 9:17 pm

Side note...

I checked my Google Webmaster Tools and I didn't have any trailing slashes indexed:

I had :

/en/blog
/en/product

but duplicates with the trailing slash didn't exist.

You can look on the 'internal links' and search for ones with the trailing slash. You can also search google for both versions of the links. I didn't see that they were both being indexed.

gsadeghi
Joomla! Intern
Joomla! Intern
Posts: 64
Joined: Sun Aug 28, 2011 9:20 pm

Re: Removing Trailing Slashes from URLs

Post by gsadeghi » Mon Apr 30, 2012 9:29 pm

lowrykun wrote:Side note...

I checked my Google Webmaster Tools and I didn't have any trailing slashes indexed:

I had :

/en/blog
/en/product

but duplicates with the trailing slash didn't exist.

You can look on the 'internal links' and search for ones with the trailing slash. You can also search google for both versions of the links. I didn't see that they were both being indexed.
That's a good point. I think Google now handle the slashes pretty well and that means there is a little chance that both URLs (w/ and w/o slashes) get indexed. However, my main reason to remove the slashes is
1) to be safe from duplicate indexing
2) to have canonical url reporting on Google Analytic and other analysis software.

On Google analytic i've seen many cases which google shows two separate data for the URL with slash and without the slash. Overall i don't think it hurts SEO as it used to before.

lowrykun
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 107
Joined: Wed Jul 20, 2011 7:02 pm

Re: Removing Trailing Slashes from URLs

Post by lowrykun » Tue May 01, 2012 4:15 pm

I noticed that this worked on the front end fine, but caused an infinite loop on the backend. It seems that on the back end it redirects:

administrator -> administrator/

Then we removed the trailing slash and redirected again.

So you'd need to make changes to exclude this - however, since we've noticed that it doesn't appear to affect Google, then I'm in favor of leaving it alone.

gsadeghi
Joomla! Intern
Joomla! Intern
Posts: 64
Joined: Sun Aug 28, 2011 9:20 pm

Re: Removing Trailing Slashes from URLs

Post by gsadeghi » Tue May 01, 2012 4:53 pm

lowrykun wrote:I noticed that this worked on the front end fine, but caused an infinite loop on the backend. It seems that on the back end it redirects:

administrator -> administrator/

Then we removed the trailing slash and redirected again.

So you'd need to make changes to exclude this - however, since we've noticed that it doesn't appear to affect Google, then I'm in favor of leaving it alone.
yes i had the same issue! i just insert a separate htaccess file on Administrator folder and it solved the issue (i don't know if it was a good idea or not but it worked!). again my main issue was working on anlaysis and it was really annoying to get data for two separate url which you know it's the same thing.

irandream
Joomla! Intern
Joomla! Intern
Posts: 52
Joined: Fri Mar 25, 2011 7:06 pm
Contact:

Re: Removing Trailing Slashes from URLs

Post by irandream » Mon May 21, 2012 5:51 pm

Some search engines remove the trailing slash from urls that look like directories - e.g. Yahoo does it. However it could result into duplicated content problems when the same page content is accessible under different urls. The solution is to create a .htaccess rewrite rule that adds the trailing slashes to these urls. Example - redirect all urls that do not have a trailing slash to urls with a trailing slash:

Code: Select all

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
Note: RewriteCond %{REQUEST_URI} !example.php
excludes a sample url that should not be rewritten. This is just an example. If you do not have a file or url that should not be rewritten, remove this line.

User avatar
nhartsog
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Wed Mar 20, 2013 8:04 pm
Location: St. Louis, MO
Contact:

Re: Removing Trailing Slashes from URLs

Post by nhartsog » Fri Mar 29, 2013 4:11 pm

This may be a dead forum thread, but just in case it helps anyone else who happens to search for this problem. My .htaccess file was removing the trailing slashes, which worked for every address except the infinite loop redirect for /administrator that lowrykun mentioned. I fixed this by entering

Code: Select all

RewriteCond %{REQUEST_URI} !administrator
directly above the already present RewriteRule to remove trailing slashes and it works perfectly. I end up with the following:

Code: Select all

RewriteBase /
#
# Remove Trailing Slash (Normally would be the opposite)
# Excluding the administrator folder to prevent infinite redirect issue.
RewriteCond %{REQUEST_URI} !administrator
RewriteRule ^(.*)/$ $1 [R=301,L]
Hope this helps someone; it was bothering me for days!

movefastbebold
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Mon Mar 25, 2013 1:51 pm
Contact:

Re: Removing Trailing Slashes from URLs

Post by movefastbebold » Tue Apr 02, 2013 1:24 pm

Hi thanks for the code, one question when you put a trailing slash on it reverts you back to the home page, is there not a way for it to stay on the same page only removing the trailing slash.

User avatar
trogladyte
Joomla! Explorer
Joomla! Explorer
Posts: 412
Joined: Sat May 03, 2008 9:27 pm
Location: Phoenix, AZ
Contact:

Re: Removing Trailing Slashes from URLs

Post by trogladyte » Fri Feb 07, 2014 7:22 pm

Thank you so much to all involved here! The trailing slashes were driving me nuts. On the first site I just did separate redirects for them all (just 5 pages) so that was easy. Next site had 100's of pages..... Not happy till I discovered this thread.

My question is why does Joomla even do this out of the box? Seems extremely stupid to automatically duplicate the pages... IMH (and somewhat unskilled) O!
Ian Shere - Phoenix Website Design & Hosting
http://www.citruskiwi.com
Certified Flight Instructor http://www.flygoodyear.com

teokolo
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Wed Oct 10, 2012 12:52 pm

Re: Removing Trailing Slashes from URLs

Post by teokolo » Fri Mar 14, 2014 9:56 am

nhartsog wrote:This may be a dead forum thread, but just in case it helps anyone else who happens to search for this problem. My .htaccess file was removing the trailing slashes, which worked for every address except the infinite loop redirect for /administrator that lowrykun mentioned. I fixed this by entering

Code: Select all

RewriteCond %{REQUEST_URI} !administrator
directly above the already present RewriteRule to remove trailing slashes and it works perfectly. I end up with the following:

Code: Select all

RewriteBase /
#
# Remove Trailing Slash (Normally would be the opposite)
# Excluding the administrator folder to prevent infinite redirect issue.
RewriteCond %{REQUEST_URI} !administrator
RewriteRule ^(.*)/$ $1 [R=301,L]
Hope this helps someone; it was bothering me for days!
I found this produces a redirect loop when accessing directories, eg: /images/

The solution is to add this line:

Code: Select all

RewriteCond %{REQUEST_FILENAME} !-d
So this is the complete code I use inside "Custom redirects" section of my .htaccess:

Code: Select all

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !administrator
RewriteCond %{REQUEST_URI} !component
RewriteRule ^(.*)/$ $1 [R=301,L]
Hope it helps someone!

EDIT: I had to add this line: to prevent invalid token on frontend login:

Code: Select all

RewriteCond %{REQUEST_URI} !component

deansseville
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Tue Nov 18, 2014 6:52 pm
Location: UK
Contact:

Re: Removing Trailing Slashes from URLs

Post by deansseville » Thu Dec 04, 2014 12:21 pm

teokolo wrote:
nhartsog wrote:This may be a dead forum thread, but just in case it helps anyone else who happens to search for this problem. My .htaccess file was removing the trailing slashes, which worked for every address except the infinite loop redirect for /administrator that lowrykun mentioned. I fixed this by entering

Code: Select all

RewriteCond %{REQUEST_URI} !administrator
directly above the already present RewriteRule to remove trailing slashes and it works perfectly. I end up with the following:

Code: Select all

RewriteBase /
#
# Remove Trailing Slash (Normally would be the opposite)
# Excluding the administrator folder to prevent infinite redirect issue.
RewriteCond %{REQUEST_URI} !administrator
RewriteRule ^(.*)/$ $1 [R=301,L]
Hope this helps someone; it was bothering me for days!
I found this produces a redirect loop when accessing directories, eg: /images/

The solution is to add this line:

Code: Select all

RewriteCond %{REQUEST_FILENAME} !-d
So this is the complete code I use inside "Custom redirects" section of my .htaccess:

Code: Select all

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !administrator
RewriteCond %{REQUEST_URI} !component
RewriteRule ^(.*)/$ $1 [R=301,L]
Hope it helps someone!

EDIT: I had to add this line: to prevent invalid token on frontend login:

Code: Select all

RewriteCond %{REQUEST_URI} !component
Genius teokolo Thanks


Locked

Return to “Search Engine Optimization (Joomla! SEO) in Joomla! 2.5”