George Aggelakis
George Aggelakis

Reputation: 11

.htaccess remove specific string at the end of url

I have an old site where the URLs are like /articles/id?PHPSESSID=

and I want to remove the ?PHPSESSID= at the end of the URL.

UPDATE: I have created a new site and the old is moved to archive.example.com. I managed to redirect the old URL to that by adding the following code to the .htaccess file:

RewriteRule ^articles/(.*)$ http://archive.example.com/articles/$1 [R=301,L]

But many old links from Google or FB have at the end the ?PHPSESSID= which can not be redirected to http://archive.example.com/ and I don't know the way to do it?

Upvotes: 0

Views: 134

Answers (1)

MrWhite
MrWhite

Reputation: 45829

RewriteRule ^articles/(.*)$ http://archive.example.com/articles/$1 [R=301,L]

To prevent the query string (ie. ?PHPSESSID=) from being passed in the redirect (ie. to remove it) then you simply need to add a ? at the end of the RewriteRule substitution. For example:

RewriteRule ^articles/(.*)$ http://archive.example.com/articles/$1? [R=301,L]

This effectively creates an empty query string on the target URL. You do not see a trailing ?. Alternatively, you can use the QSD (Query String Discard) flag on Apache 2.4+

Note that this removes the entire query string, not just the PHPSESSID parameter.

Upvotes: 1

Related Questions