Yoast: RT @idivenow: I just donated to @Yoast for his awesome plugins, you should too: http://t.co/7GTWe1rE <-- THX!!
![]()
PHPSESSID in your URL? Learn to 301 redirect them with PHP
I'm doing some work on a site which has like 4,500 pages indexed with a PHPSESSID in the URL, causing some major duplicate content problems. I got the server admin to disable the PHPSESSID's by adding the following to the vhost config:
php_value session.use_trans_sid 0 php_value session.use_only_cookies 1
I also wanted Google to get a clean URL when it decided to spider one of the old URL's again, and didn't have access to mod_rewrite, so I redirected them with some PHP. The solution is quite simple:
if (isset($_GET['PHPSESSID'])) {
$requesturi = preg_replace('/?PHPSESSID=[^&]+/',"",$_SERVER['REQUEST_URI']);
$requesturi = preg_replace('/&PHPSESSID=[^&]+/',"",$requesturi);
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://".$_SERVER['HTTP_HOST'].$requesturi);
exit;
}



by Arjan Eising on 30 March, 2007 at 19:12
Thanks for sharing this Joost. Note that if you haven't access to the ini files, this parameters can set by using the ini_set function.
by Joost de Valk on 30 March, 2007 at 19:34
very true Arjan, thx! (and sorry, I edited your comment to change the link to php.net instead of nl.php.net :) )
by Arjan Eising on 30 March, 2007 at 19:58
Ah damn, sorry for that one. PHP.net automatically redirects to the Dutch version, so I didn't notice it.
by Joost de Valk on 30 March, 2007 at 20:00
no probs, I just know they don't like it when you link to a local mirror :)
Pingback: How to remove www from your URL with mod_rewrite - SEO and Webdesign Blog - Joost de Valk
by Jens on 2 July, 2007 at 15:39
Short and sweet. I really have problems with Session-IDs in WordPress. I will try your hint! But I really wonder, that there is no wordpress plugin for the redirection of session-id-spamed urls... should not be too difficult...
Pingback: Can i have old files in a site which will not be included in link? - Google Community
by Dainų tekstai on 13 December, 2007 at 10:43
Thanks for it. Very useful. But there is any solution with PhP to leave previos url when page redirect to new page?
by Chris Brown on 14 January, 2008 at 19:06
Thanks for sharing. Very nice trick :)
by fra on 4 February, 2008 at 09:08
hi joost
i am having the same problem here. since i dont know anything about coding i wanted to ask you if i have to place the second code you provided in this post in the php.ini file as well...thanks!
by Robert on 31 March, 2008 at 14:13
I got this problem:
Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 0 in ...
and solved it like that:
$requesturi = preg_replace('/&PHPSESSID=[^&]+/',"",$_SERVER['REQUEST_URI']);
$requesturi = preg_replace('/PHPSESSID=[^&]+/',"",$requesturi);
Pingback: social media training » Removing ?PHPSESSID from a URL :: StumbleUpon Update
by OK85 on 23 April, 2008 at 09:12
But by using the redirection you will loose all data stored in sessions, don't you?
by Crispijn on 7 June, 2008 at 08:50
Very nice feature! I solved the prob really quick!
Pingback: SlideShare should get it's SEO fixed - Yoast - Tweaking Websites
by Milan on 23 March, 2009 at 00:19
And how to stop WordPress and other scripts from creating cookies with PHPSESSID? I don't see their use and they are only wasted bandwidth.
by Scritube on 31 March, 2009 at 08:09
PHPSESSID can't be used for that.
but try on you header :
by Boogie on 16 April, 2009 at 13:41
Hi, thanks für this easy to use solution.
I modified the regexp code:
$requesturi = preg_replace('/[.|?|&]PHPSESSID=[^&]+/',"",$_SERVER['REQUEST_URI']);
by Misko on 14 May, 2009 at 10:18
Hi!
Where did you change/put that code exactly? Where is the "vhost config"?
thanx a lot!