I need a solution. For some reason in the past seems that I generate some "bad" links for bots only.
Resume: There is a fake "page" parameter when malformed url is present. When there are 2 "page" parameters then the first one is fake, must be removed.
- X is random
- Remove the parameter page only when "?/?page" is detected
Good: search?pagepage=496
Bad: search?/?page=X
Good: https://example.com/search?page=496
Good: https://example.com/search?page=496&orderBy=oldest
Bad: https://example.com/search?/?page=X&page=496&orderBy=oldest
RewriteCond %{QUERY_STRING} ^(.*)&?^XXX[^&]+&?(.*)$ [NC]
RewriteRule...
Thank you guys!
UPDATE
At final, I found a solution by myself:
RewriteCond %{QUERY_STRING} ^(.*)&?^/\?page=[^&]+&?(.*)$ [NC]
RewriteRule ^/?(.*)$ /search$1?%1%2 [R=301,L]
This will do the rewrite for all your URLs that have the extra
pageparameter you want to keep.To make the last part optional, we would have to wrap
&(page=.*)into another set of braces, and add a?as quantifier -(&(page=.*))?. Then the back reference would need to be changed from%1to%2(because we only need that inner part, we don't want the&) - but then for your URL without any realpageparameter to keep, there would be no match in this place, and therefor the%2would not be substituted with anything, but added to the URL literally.So better to leave the above as-is, and simply add
below those two existing lines. The pattern does not need to be any more specific (because the URLs that have a genuine
pageparameter at the end, have already been handled by those previous two lines.) And theQSDmakes it simply drop the existing query string, so thathttps://example.com/search?/?page=20results inhttps://example.com/search(which I assume is what you wanted here, because there is no actual page parameter to keep, correct?)