1

Hello I have been searching the web for an answer to this but I can not find it. I am looking to use htaccess to strip a specific parameter from the url.

For example, I have the following urls:

www.mysite.com/product.php?product_id=123&session=sdfs98d7fs9f8d7
www.mysite.com/anotherurl.php?session=12312341341&someotherparam=123

I would need them to 301 redirect to:

www.mysite.com/product.php?product_id=123
www.mysite.com/anotherurl.php?someotherparam=123

Note the urls above are just examples. Ill need the session param removed from any and all urls no matter how many params are part of the url or where session is located.

I think I need a way to the url string up to session=blah and the url string after session=blah, then combine both parts and redirect to the new url.

Any help is greatly appreciated, thanks.

3 Answers 3

1

UPDATED

In short, what you want is just to remove the key-value pair session=xx from the query.

Here is an option:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING}  (.*)?&?session=[^&]+&?(.*)?  [NC]
RewriteCond %{REQUEST_URI} /(.*)
RewriteRule .*  %3?%1%2? [R=301,L]
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. Yeah the urls would be dynamic the two I had were just examples I just need the session param removed from any and all urls
Thanks faa. It doesn't look like your updated answer would work if the other parameter is not product_id or had more than one valid parameter. I think I'm looking for something that would take any url before session=blah and any url after session=blah and then redirect to the combined parts.
@bones Updated my answer again.
Thanks faa. It's not quite working. When I have the url: www.mysite.com/catalog/product_info.php?products_id=2894&session=d794705 it redirects to www.mysite.com/products_id=2894 I would need it to redirect to www.mysite.com/catalog/product_info.php?products_id=2894
¿Is the script name also variable or it is now product_info.php instead of product.php ?
|
1

I found a solution on another site

# case: leading and trailing parameters
RewriteCond %{QUERY_STRING} ^(.+)&session=[0-9a-z]+&(.+)$ [NC]
RewriteRule (.*) /$1?%1&%2 [R=301,L]

# case: leading-only, trailing-only or no additional parameters
RewriteCond %{QUERY_STRING} ^(.+)&session=[0-9a-z]+$|^osCsid=[0-9a-z]+&?(.*)$ [NC]
RewriteRule (.*) /$1?%1 [R=301,L]

Comments

0

not tested, but should work:

RewriteCond %{QUERY_STRING} product_id=([0-9]+)
RewriteRule .* product.php?product_id=%1

1 Comment

Thanks. I guess I should have mentioned the irks could be totally different. The common denominator being the session param which I need removed from any URL. I've adjusted the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.