Remove SID query from Magento URLs

19 Comments 9th JUL 2009 | Posted by Tomislav Bilic in Magento, Tools

Remove SID query from Magento URLs

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

If you like what you read, please share it.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Yahoo! Bookmarks
  • Reddit
  • Technorati
  • Twitter
  • StumbleUpon
  • LinkedIn
  • Netvibes
  • FriendFeed
  • NewsVine
  • Sphinn
  • Tumblr

There are 19 comments (Add Yours +)

  • 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}?

  • HM, no… That should be it. Can you tell me what site do you have?

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

  • Hey Phil. Good site you have. Yeah, you are right. The dots (.) should be escaped with \. That’s what’s mission on Yoast side.

  • 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!

  • Toni Anicic Says

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

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

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

  • Ahamed sha Says

    it helped me. Thank You

  • 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)

  • 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!

  • 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,

  • anders Nielsen Says

    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…

  • anders Nielsen Says

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

  • 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!

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

  • @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

  • Amazing, it saved me lot of time…
    Thanks for your hard work.

  • 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

Leave a Comment

Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009