0

i have a bunch of miss-generated URLs, which i would like to redirected to the correct URL.

This is a good example:

http://example.com/search/?q=searchterm%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F&scope=45%2F%2F%2F%2F%2F/

I already could redirect most of them with the following rule:


    RewriteCond %{QUERY_STRING} ^(.*)%2F
    RewriteRule ^search/$ /search/?%1 [NE,R=301,L]

As i understand, this only works because the trailing "%2F" is removed and then the rule hits again. Google only accepts a limited number of redirects.

So i try to adapt the rule to work with 1 or more occurrences of "%2f"

This is how far i got:


    RewriteCond %{QUERY_STRING} ^(.*)((%2F)*)
    RewriteRule ^search/$ /search/?%1 [NE,R=301,L]

The results are: /search/?q=searchtermFFFFFFFFFFFFFFFF&scope=45FFFFF/

What am i missing?

Best regards, Alexander

1 Answers1

1

If you only have two query parameters, this may work (of course it can be adapted if you have more or less):

# %1 Matches everything until the first group of %2F
# %2 Matches everything after the first group %2F until the second group
RewriteCond %{QUERY_STRING} ([^%2F]*)[%2F]*([^%2F]*)[%2F]*
RewriteRule ^search/$ /search/?%1%2 [NE,NC,R=301,L]

Hope that helps.

clmarquart
  • 4,721
  • 1
  • 27
  • 23