Page 1 of 1
[solved] How to remove trailing slash from URLs?
Posted: Sat Feb 22, 2014 12:12 am
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
Re: How to remove trailing slash from URLs?
Posted: Sat Feb 22, 2014 4:17 am
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...
Re: How to remove trailing slash from URLs?
Posted: Sat Feb 22, 2014 11:49 am
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.
Re: How to remove trailing slash from URLs?
Posted: Sat Feb 22, 2014 5:34 pm
by Rolf
Re: [solved] How to remove trailing slash from URLs?
Posted: Sat Feb 22, 2014 10:08 pm
by magallo
Thank you. All i had to do was really just modify the config.php file
$config['page_extension'] = '';
Re: [solved] How to remove trailing slash from URLs?
Posted: Sun Feb 23, 2014 6:02 pm
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
Re: [solved] How to remove trailing slash from URLs?
Posted: Tue Mar 04, 2014 9:27 pm
by Dr.CSS
I personally recommend using page extensions as it most likely will remove the /...