homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

?PHPSESSID redirect

 12:22 am on Jul 8, 2007 (gmt 0)


I have been working on getting an unusual PHPSess string removed from the front page of my site. I believe this may be causing duplicate issues. It's a tricky one and unfortunatley I haven't been able to get it working.

I need to 301 redirect the following string to the frontpage:


to just www.site.com/

Using something along the lines of the following did not work:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^PHPSESSID=f9f2770d591366bc$
RewriteRule ^/$ http://www.example.com/?productid=100002&cat=&page=1 [R=301,L]

Can anyone pinpoint an error in the above?



 5:59 pm on Jul 8, 2007 (gmt 0)

Because you've used the "Options" directive, I assume that this code goes into .htaccess instead of going into httpd.conf or conf.d. If that is the case, remove the slash fro the RewriteRule pattern, because it will not be present in the URL-path that RewriteRule examines:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^PHPSESSID=f9f2770d591366bc$
RewriteRule [b]^$[/b] http://www.example.com/?productid=100002&cat=&page=1 [R=301,L]

Now I uspsect a furhter problem, in that this rule takes (exactly) example.com/PHPSESSID=f9f2770d591366bc and redirects it to example.com/?productid=100002&cat=&page=1

In other words, it remove the session ID and then creates the productid=100002, blank category, and page=1 name/value pairs in the query string.

So, it will work for only the one session ID, and will always set your query string to te value specified, regardless of what it might have contained in addition to the session ID.

I suspect that what you are really trying to do is to remove the session ID but preserve the other parameters, and that you'd like to remove *any* session ID, and not just the one exact session ID number you've specified above.

I also presume you only want to remove the session ID for search engine robots, since your site probably won't work if you remove them unconditionally.

In that case, you'd want something more like:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} Googlebot [OR]
RewriteCond %{HTTP_USER_AGENT} Slurp [OR]
RewriteCond %{HTTP_USER_AGENT} msnbot [OR]
RewriteCond %{HTTP_USER_AGENT} Teoma
RewriteCond %{QUERY_STRING} ^(([^&]+&)+)*PHPSESSID=[0-9a-f]*&(.*)$
RewriteRule ^$ http://www.example.com/?%1%3 [R=301,L]

This will redirect to remove the hexadecimal-number session ID from your "home page" URL when it is requested by major SE robots, but leave any other query string variables intact.

Be aware that this whole rewriterule redirect method is an after-the-fact "repair" approach; You should modify the script(s) you are using to make sure that recognized search engine spiders are never given a session ID in the first place, and use the code above only to repair the "messy" search results that you have now.



 11:21 pm on Jul 8, 2007 (gmt 0)

Thanks for that information. Yes, I have repaired the original code to not produce PHPSESSID's. All I have to do now is clean up the search engine results - and this is the last one remaining!

I actually posted some partly incorrect code. What I MEANT to type was:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^PHPSESSID=f9f2770d591366bc$
RewriteRule ^$ http://www.example.com/ [R=301,L]

As you can see, all I wanted to do was to refer that exact PHPSESSID to a domain, and nothing further. I must have had a Sunday morning hangover :-/

HOWEVER, when uploaded and accessed, the page is not responding (perhaps going into an endless loop?).


 11:37 pm on Jul 8, 2007 (gmt 0)

To remove the current query string, you'll need to add a "?" to the substitution URL:

RewriteCond %{QUERY_STRING} ^PHPSESSID=f9f2770d591366bc$
RewriteRule ^$ http://www.example.co[b]m/?[/b] [R=301,L]

The "?" serves as a "operator" to clear the query, and/but will not show up in the redirected URL.



 11:44 pm on Jul 8, 2007 (gmt 0)

That works perfectly.

Thanks to people like you, I think I am finally "getting" mod rewrite. Some inspiration to do some more self-learning.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved