Remove SID query from Magento URLs

Featured Image

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. :)

Interested in hiring us?

Have a chat with us. You would be surprised how small changes can make your business even more successful.


About Tomislav Bilic

Founder and CEO

Tomislav is a founder and CEO at Inchoo. Enjoys traveling - especially quick getaways, traditional cuisine (from most cultures), good wine and strong rakija.

Read more posts by Tomislav / Visit Tomislav's profile

42 comments

  1. I had a weird problem with the SID on frontend option today. I have an store with this option enabled, and when access a product page it was causing an inifinite loop. I noticed that this just happend when Block page caching was enabled.
    I also noticed that when accessing the product.html address, magento adds the ___SID=U on the looping url.

    I solved the problem by disabling the SID on frontend option.

  2. I have a Magento GO e-store – small one ;-)
    With the Site:designdanois.fr i get 811 hit. But I have 26 real url in my store. (yes it is small)

    I can see a lot of SID on all the extra url’s.

    Do some one know, how to change that in a GO e-shore. I don’t know if I can access the code and I am new in the “game”. But all this extra SID url is killing my e-shop in google ;-(

  3. I am wondering how I would rewrite rule to remove sid and always force www when using mutlistore setup.

  4. I have been having a complete nightmare with this exact same thing. You’ll want to throw in both fixes…If you enter your store without the URL matching the set base url then magento will override adding the SID – in my case adding ?frontent=! So bring this into the htaccess – before the request gets to magento, and if google has already indexed some of these urls do a bit of rewriting to let google and others know to update and at the same time keep any other params in the get request and also accounting for SSL!

    :: Put in the top of your .htaccess (swap out frontend for SID if thats what you getting issues with ::

    ############################################
    # Do redirecting to www. here, if its done by magento it will add ?SID/?frontend=

    RewriteEngine on

    # Handle non ssl
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.co\.uk$ [NC]
    RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]

    # Handle ssl
    RewriteCond %{HTTPS} =on
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.co\.uk$ [NC]
    RewriteRule ^(.*)$ https://www.mydomain.co.uk/$1 [R=301,L]

    # Remove the query part if it comes through from existing link and has other params
    RewriteCond %{QUERY_STRING} ^frontend=([^&]*)&(.*)$
    RewriteRule (.*) /$1?%2 [R=301,L]

    # Remore the qurey part fully if its just the sid present
    RewriteCond %{QUERY_STRING} ^frontend=([^&])
    RewriteRule (.*) /$1? [R=301,L]

    Hope this helps :)

  5. A solution for those who want to keep all their variables but want to remove only the SID (The first variable should be the SID)

    RewriteCond %{QUERY_STRING} SID=([^&]*)&(.*)
    RewriteRule (.*) /$1?%2 [R=301,L]

  6. We run several stores do i need to make these changes

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com$ [NC]
    RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

    for every store or just the main magento installation ?

  7. An alternative method to modifying the htaccess file is to change the “feature” directly in the Magento backend.

    In CE v1.4.2.0 (and perhaps earlier versions) you can turn this feature off here:

    System > Configuration > Web > Session Validation Settings > Use SID on Frontend (No)

    Hope that helps!

  8. 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

  9. 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

  10. 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]

  11. 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]

  12. 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.

  13. 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.

  14. 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.

  15. 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]

  16. 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

  17. 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.

  18. 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??

  19. 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…

  20. 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!

  21. Actually my last post doesn’t correctly redirect ww. and www. but does redirect non-www to www. Maybe someone can adapt it…

  22. 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 :)

  23. Lord Robert, you’re right, that is a common mistake people tend to make. Thank you for your contribution.

  24. 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!

  25. 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.

  26. 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}?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.