[solved] How to remove trailing slash from URLs?

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
User avatar
magallo
Dev Team Member
Dev Team Member
Posts: 121
Joined: Thu Mar 24, 2011 12:37 am

[solved] How to remove trailing slash from URLs?

Post by magallo »

I would like to remove the last trailing slash from URLs. When using the pretty url config setup and the .htaccess provided urls are like:

http://mydomain.com/blabla/

how do i remove that last slash to achieve:

http://mydomain.com/blabla

here is my htaccess code:

Code: Select all

<IfModule mod_rewrite.c>
RewriteEngine on
#
#Sub-dir e.g: /cmsms
RewriteBase /

# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
# but ignore POST requests.
#RewriteCond %{REQUEST_URI} !/$
#RewriteCond %{REQUEST_URI} !\.
#RewriteCond %{REQUEST_METHOD} !POST$
#RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
thanks for the help
Last edited by magallo on Sat Feb 22, 2014 10:07 pm, edited 1 time in total.
Magal Hezi
Pixel Solutions, Technology Partners
magal@pixelsolutions.biz | pixelsolutions.biz

Image
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm
Location: OH, USA

Re: How to remove trailing slash from URLs?

Post by JohnnyB »

#RewriteCond %{REQUEST_URI} !/$
#RewriteCond %{REQUEST_URI} !\.
#RewriteCond %{REQUEST_METHOD} !POST$
#RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

That should do it unless you have a forward slash set as your page extension in the config.php file.

Keep in mind it is best practice to keep the trailing slash. It is important to speed up (not noticeable to you) the loading time of a page. Because without it, the browser will first look for a file that doesn't exist. Then a redirect occurs. And depending upon the redirect you might get the good 301 Redirect, or you might get a less SEO worthy 302 Redirect, and worse yet, it might give a 404 first which would be bad...

Whatever you choose to do, make sure it matches what the canonical url link is. For example, if you use:
;http://www.domain.com/some/path

make sure the canonical does the same...

Google will treat ;http://www.domain.com/some/path and ;http://www.domain.com/some/path/ as two different pages...
Last edited by Dr.CSS on Tue Mar 04, 2014 9:26 pm, edited 1 time in total.
Reason: Please use double quotes or something on fake links so they aren't clickable...
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am
Location: The Netherlands

Re: How to remove trailing slash from URLs?

Post by velden »

Thanks for your explanation.

However I doubt that this is true:
Keep in mind it is best practice to keep the trailing slash. It is important to speed up (not noticeable to you) the loading time of a page. Because without it, the browser will first look for a file that doesn't exist. Then a redirect occurs. And depending upon the redirect you might get the good 301 Redirect, or you might get a less SEO worthy 302 Redirect, and worse yet, it might give a 404 first which would be bad...
A browser only requests, it does not search.
In the 'pretty url case' the webserver might search for the file because it's explicitly told to do so

Code: Select all

RewriteCond %{REQUEST_FILENAME} !-f
It will search for a directory too

Code: Select all

RewriteCond %{REQUEST_FILENAME} !-d
And finally will serve the index.php (assuming neither file nor directory exists). No 301, 302 involved here.

Rest of explanation is very helpful IMO.
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Location: The Netherlands
Contact:

Re: How to remove trailing slash from URLs?

Post by Rolf »

In config.php

Code: Select all

$config['page_extension'] = '';
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
User avatar
magallo
Dev Team Member
Dev Team Member
Posts: 121
Joined: Thu Mar 24, 2011 12:37 am

Re: [solved] How to remove trailing slash from URLs?

Post by magallo »

Thank you. All i had to do was really just modify the config.php file

$config['page_extension'] = '';
Magal Hezi
Pixel Solutions, Technology Partners
magal@pixelsolutions.biz | pixelsolutions.biz

Image
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm
Location: OH, USA

Re: [solved] How to remove trailing slash from URLs?

Post by JohnnyB »

Slightly off topic with the SEO and trailing slash stuff - sorry!

There are a differences of opinion to a trailing slash or not and it would be good for folks to research and find what works best for them.
For me, I almost always make sure a trailing slash is used especially because browsers almost always add a trailing slash to an actual physical directory path. It is a just a way to present consistency.

Which is the most important thing - consistency - regardless of your preference.

Here are some sources to consider:
http://googlewebmastercentral.blogspot. ... slash.html
http://www.tricksbag.com/seo/trailing_s ... tters.html
http://goo.gl/AOZfrB
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am
Location: Arizona

Re: [solved] How to remove trailing slash from URLs?

Post by Dr.CSS »

I personally recommend using page extensions as it most likely will remove the /...
Post Reply

Return to “The Lounge”