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:
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:
Joost is an internet entrepreneur and the founder of Yoast. He has a long history in WordPress and digital marketing. On our blog, he has written a lot about SEO in general, technical SEO and important topics related to SEO.
November 21 - 22, 2025
Team Yoast is Attending, Organizing, Sponsoring at WordCamp Pisa 2025! Click through to see who will be there, what we will do, and more!
See where you can find us next »
20 November 2025
Learn how to start your SEO journey the right way with our free webinar. Perfect for beginners seeking to improve website performance 🚀 .
All Yoast SEO Webinars
Discussion (19)
MiskoMay. 14, 2009
Hi!
Where did you change/put that code exactly? Where is the “vhost config”?
thanx a lot!
BoogieApr. 16, 2009
Hi, thanks für this easy to use solution.
I modified the regexp code:
$requesturi = preg_replace(‘/[.|?|&]PHPSESSID=[^&]+/’,””,$_SERVER[‘REQUEST_URI’]);
ScritubeMar. 31, 2009
PHPSESSID can’t be used for that.
but try on you header :
MilanMar. 23, 2009
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.
CrispijnJun. 07, 2008
Very nice feature! I solved the prob really quick!
OK85Apr. 23, 2008
But by using the redirection you will loose all data stored in sessions, don’t you?
RobertMar. 31, 2008
I got this problem:
Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 0 in …
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!
Chris BrownJan. 14, 2008
Thanks for sharing. Very nice trick :)
Dainų tekstaiDec. 13, 2007
Thanks for it. Very useful. But there is any solution with PhP to leave previos url when page redirect to new page?
JensJul. 02, 2007
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…
Joost de ValkMar. 30, 2007
no probs, I just know they don’t like it when you link to a local mirror :)
Arjan EisingMar. 30, 2007
Ah damn, sorry for that one. PHP.net automatically redirects to the Dutch version, so I didn’t notice it.
Joost de ValkMar. 30, 2007
very true Arjan, thx! (and sorry, I edited your comment to change the link to php.net instead of nl.php.net :) )
Arjan EisingMar. 30, 2007
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.
Hi!
Where did you change/put that code exactly? Where is the “vhost config”?
thanx a lot!
Hi, thanks für this easy to use solution.
I modified the regexp code:
$requesturi = preg_replace(‘/[.|?|&]PHPSESSID=[^&]+/’,””,$_SERVER[‘REQUEST_URI’]);
PHPSESSID can’t be used for that.
but try on you header :
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.
Very nice feature! I solved the prob really quick!
But by using the redirection you will loose all data stored in sessions, don’t you?
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);
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!
Thanks for sharing. Very nice trick :)
Thanks for it. Very useful. But there is any solution with PhP to leave previos url when page redirect to new page?
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…
no probs, I just know they don’t like it when you link to a local mirror :)
Ah damn, sorry for that one. PHP.net automatically redirects to the Dutch version, so I didn’t notice it.
very true Arjan, thx! (and sorry, I edited your comment to change the link to php.net instead of nl.php.net :) )
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.