Remove SID query from Magento URLs
29 Comments 9th JUL 2009 | Posted by Tomislav Bilic in Magento, Tools

Many people wonder why sometimes the SID part appears in their Magento URLs. This is when your URL has additional SID query usually at the end. Take a look at the image. The curiosity is that it does not appear always. What is the most common scenario it happens? You didn’t access the site with the same domain variant you entered as your “Base URL” in your System> Configuration> Web interface.
When you decide to launch the site, you have to decide whether you will market http://www.domain.com/ URL or http://domain.com/. This is an important decision and you shouldn’t change your mind quite often. Search engines usually treat those two URLs as a different sites and therefore the Page Rank potential can be split between those two URLs. So, think about whether you will use www or not and stick by this decision.
Once you decided, go to your System> Configuration> Web interface and enter the desired form to “Base URL” field. When you access the site you will notice that there are no “SID”s when the URL matches the value from “Base URL” field and they appear when it does not.
Now, we want the ability that the site redirects to proper URL once accessed. Someone can place a wrong link to some forum or blog. We don’t want those links to lead to improper URL and we don’t want SIDs to appear to those visitors. Most important: We don’t want that search engines index the URLs with SIDs.
The solution is simple. Go to your .htaccess fine and find the line that says
RewriteEngine on
If you want to have www part:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
If you don’t want to have www part:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]
Btw. This is applicable and good to be set to every site, not just Magento ones.
To post code in comments, place your code inside [code] and [/code] tags.


















July 9th, 2009 at 15:13
Thanks for this but I can’t get it to work. Obviously you replace ‘yourdomain’ with your actual domain url. Does anything else need setting e.g. {HTTP_HOST}?
July 9th, 2009 at 15:23
HM, no… That should be it. Can you tell me what site do you have?
July 9th, 2009 at 17:14
I spoke too soon! You are a star!!
I knew already that this was best practice and actually spent a couple of hours the other day trying to achieve this redirect with code from Yoast’s site, but failed to get it to work.
I could not at first see the difference between his and your recommended redirect code, so asked my first question as an instant response to your new post.
As soon as I pasted your code it worked straight away!
Any chance you could recommend code to redirect index.php?
Our Magento website is http://www.use-IP.co.uk
Many Thanks.
July 10th, 2009 at 10:40
Hey Phil. Good site you have. Yeah, you are right. The dots (.) should be escaped with \. That’s what’s mission on Yoast side.
July 10th, 2009 at 15:40
While your at it you can really help your visitors out from making the silly mistake of missing of a “w”, or double tapping a “w”.
i.e. ww.yourdomain.com / wwww.yourdomain.com
Really easy, make the ww. and wwww. subdomain point to the same location as the main domain on the server and simply…
RewriteCond %{HTTP_HOST} ^yourdomain\.co\.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^ww\.yourdomain\.co\.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^wwww\.yourdomain\.co\.uk$ [NC]
RewriteRule ^(.*) http://www.yourdomain.co.uk/$1 [L,R=301]
Then when anyone accidently types ww.yourdomain.co.uk, they get sent to the correct URL insted of “Page cannot be displayed”
Hope it helps!
July 13th, 2009 at 7:44
Lord Robert, you’re right, that is a common mistake people tend to make. Thank you for your contribution.
July 17th, 2009 at 0:52
Or:
# ensures all links www, localhost etc.
RewriteCond %{HTTP_HOST} !^(www\.|mail\.|shell\.|localhost|127\.0\.0\.1) [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Like that when anyone types anything that isn’t in the list, they get redirected.
And, this doesn’t need you to remember to re-code the website name when you copy the htaccess to a new website
July 17th, 2009 at 1:00
Actually my last post doesn’t correctly redirect ww. and www. but does redirect non-www to www. Maybe someone can adapt it…
August 23rd, 2009 at 0:33
it helped me. Thank You
September 8th, 2009 at 9:02
Thanks for the great info. However, do you know what the code would be with a .com.au domain and the subdirectory of /store?
(ie: mydomain.com.au/store directed to http://www.mydomain.com.au/store)
September 10th, 2009 at 13:50
I just did this to my .htaccess file on my magento site, and now I can’t access the admin area. I get a 404 error! Help!
September 11th, 2009 at 3:42
Can anyone tell me how to change the permission to .htaccess file on server? I cannot modify it since it has 644 permission on that
my store is http://www.invitebyvoice.com
Regards,
November 18th, 2009 at 22:29
It’s not working when using IDN domains ( parked ) I use more than 4 domains, they all create SID, because you have to use the punycode url in the index.php, and in the backend you use “the good looking url” for seo and more nice link structure…
Then SID=45454 are created… But the rewrites are not working in this scenario…
Would love to get i work…
November 18th, 2009 at 22:57
Anyone knows how to remove SID’s when using my scenario???
I only have one httacces file to do rewrites in… any solutions for this??
December 5th, 2009 at 17:31
Hello! I dont have sucess on that.
my base domain is http://www.suricate.com.br
my secure domain is suricate1.sslblindado.com.br
so i tried to add these lines to my .htaccess
RewriteCond %{HTTP_HOST} !^www\.suricate\.com\.br$ [NC]
RewriteRule ^(.*)$ http://www.suricate.com.br/$1 [R=301,L]
but have an error when switch from base to secure domain:
Incorrect Redirect
What can i do?
Thanks!
January 16th, 2010 at 22:55
Thank you for this. The SID issue was perplexing and driving me crazy. Compounding the problem was that not only was the long SID present, but at the very end it also included the SEO friendly URL text for the page.
January 29th, 2010 at 17:31
@Suricate- Your issue must redirect based on the SSL flag.
# Prepend WWW if SSL off
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\.suricate\.com\.br$ [NC]
RewriteRule ^(.*)$ http://www.suricate.com.br/$1 [R=301,L]
#Prepend WWW if SSL on
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} !^www\.suricate1\.sslblindado\.com\.br$ [NC]
RewriteRule ^(.*)$ https://www.suricate1.sslblindado.com.br/$1 [R=301,L]
cheers
esjabe
February 14th, 2010 at 14:50
Amazing, it saved me lot of time…
Thanks for your hard work.
March 8th, 2010 at 14:27
Hi Thanks for you Help.
But whats the best config for a magento Multistore?
myshop.net/firstkategorie/home
myshop.net/firstkategorie/
myshop.net/
myshop.net/index.php
Are the same
April 30th, 2010 at 11:21
Hi,
Can someone help me (novice) out here, I’m trying to set this up for a .co.uk site but having no luck… I presumed below is correct?
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [R=301,L]
April 30th, 2010 at 11:35
Tom, if you wanna keep the www part you’ll need this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.co.uk/$1 [R=301,L]
So yea, what you wrote is correct as far as I can see.
April 30th, 2010 at 12:36
Thanks for your quick response Toni.
I have tried your suggestion but it doesn’t seem to be having any effect… I had to make the htaccess.txt file and I put it in my public_html.
April 30th, 2010 at 12:59
Tom,
Did you rename the “htaccess.txt” into “.htaccess”?
Between, you should already have a .htaccess file on your Magento installation by default, you just need to edit that one.
It might be invisible to you depending on which FTP client are you using and it’s settings.
May 4th, 2010 at 12:19
to remove the indexed SIDs put in htaccess:
#kill those indexed SIDs
RewriteCond %{THE_REQUEST} \?(.*)SID(.*)\ HTTP/ [NC]
RewriteRule (.*) /$1? [R=301,L]
to make .co.uk domains working with non-www to www rewrtie you have to escape the second dot, in \.co\.uk instead of just \.co.uk:
#non-www to www.
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.sitename\.co\.uk
RewriteRule (.*) http://www.sitename.co.uk/$1 [R=301,L]
May 4th, 2010 at 12:28
David,
Nice, thanks for contributing.
June 13th, 2010 at 20:26
i have the following in the .htaccess but is not redirecting… do i missed anything?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com\.cy$ [NC,OR]
RewriteCond %{HTTP_HOST} ^ww\.domain\.com\.cy$ [NC,OR]
RewriteCond %{HTTP_HOST} ^wwww\.domain\.com\.cy$ [NC]
RewriteRule ^(.*) http://www.domain.com.cy/$1 [L,R=301]
July 26th, 2010 at 12:15
Still waiting. no reply :/
anyone know to help me?
July 29th, 2010 at 9:52
Hi,
I am facing same problem, and was not able to resolve it using this post.
I checked base url and url in browser they are same.
I get this error when i redirect to payment gateway and my caching is on.
I will be grateful if you can provide me any help on this.
Thanks & Regards,
Saurabh
August 23rd, 2010 at 10:48
Very useful thread, but I need a solution for a multi-store setup and can’t figure out how to do it.
Applying the single site version multiple times doesn’t work and doing nothing currently means that non-www version of all except one of the sites redirects to the first site created on Magento.
Any ideas anyone?
Thanks in advance