Hi folks,
I have a situation that's most easily explained via example, I think. So here goes. Let's say I have the following URLs:

  1. /page.asp?id=123
  2. /page.asp?id=123&category=ABC
  3. /page.asp?id=123&category=ABC&var3=X&var4=Y

I need these pages redirected. Currently links 1, and 2 redirect correctly. Option 3 though can have an arbitrary number of other parameters with arbitrary values. What I need to do is, for page.asp, ignore all parameters aside from id and category. Is there a way to do this with rewrite rules? If not, would it be possible to do if I did have the names of the other parameters?

Not sure it helps, but here is what I have so far. unwanted_var_name and value are replacing specific param and value that we're presently using to bulk redirect anything with that parameter... however this isn't ideal:

RewriteCond %{QUERY_STRING} unwanted_var_name=value
RewriteRule .*$ http://www.domain.com? [R,L]

Thanks,
EF

Try this:

RewriteCond %{QUERY_STRING} (id=(.+?)&)(category=(.+?)&) [NC]
RewriteRule ^page.asp(.*)$ page.asp?%1category=%2 [R=301,last]

Thanks mattster! This looks like it might work. I won't be able to implement until Monday, but if I get it working then I will mark this as solved. Also going to try to figure out how it works -- not a regex pro by any stretch of the imagination ^.^;

No worries, you might find you only need the second line - I can't quite remember and haven't had a chance to test it yet.

BTW, if you're ever stuck with regex, look at this: https://www.regex101.com/

It's a great testing tool for a whole load of languages, but allows you to experiment and walks you through everything that happens during validation. I'm sure you will have seen there is no end of help with Regex' out there if you want something fairly specific.

Rep is greatly appreciated if I helped at all :)

Thanks again mattster. I have a couple questions though. I'm using http://htaccess.madewithlove.be/ to test the URLs.

  1. If I run this URL: http://www.domains.com/page.asp?id=944&category=119&utm_source=X&utm_medium=Y
    I get this: http://www.domains.com/page.asp?id=944&category=944
    As you can see the 944 from the id is repeating twice. I tried %3 instead of %2, that gave me category=category=119&. It looks like %4 gets it right, but I'm not sure how that works.

  2. Will the order of the parameters for this matter? What if id or category are not the first parameters? What if id or category are missing, with only one of them showing?

Yes sorry, the query string splits it up in the following way:
%:

  1. id=944& (full ID string and the end "&" character in the specified URL)
  2. 944 (ID value)
  3. category=119& (full category string and the end "&" character given in the specified URL)
  4. 119 (Category)

You can change it to this: (neater but has trailing &)

RewriteCond %{QUERY_STRING} (id=(.+?)&)(category=(.+?)&) [NC]
RewriteRule ^page.asp(.*)$ page.asp?%1%3 [R=301,last]

Or this: (longer, without trailing &)

RewriteCond %{QUERY_STRING} (id=(.+?)&)(category=(.+?)&) [NC]
RewriteRule ^page.asp(.*)$ page.asp?id=%2&category=%4 [R=301,last]

Yes the order of the parameters do matter, if they are in the wrong order the query string will not be met, and the URL will not be processed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.