Strange Characters at end of URL and 301 wont redirect them to correct url

46 Views Asked by At

The below weird URLs popped up on Google console...

I didnt add them and I dont know where they came from.

  • https://www.example.com/gallery.html?page=3
  • https://www.example.com/gallery.html?page=2
  • https://www.example.com/gallery.html?page=1

My 301 redirects in .htaccess are working for other pages but not these above URLs

Can anyone help me, I want to redirect them to /gallery.html

Added to my .htaccess which works for other redirects

RedirectMatch 301 /gallery.html?page=1 /gallery.html
1

There are 1 best solutions below

6
MrWhite On

The software that made those pages is gone and its not needed. I just want the Gallery.html page operating

(I assume you mean gallery.html, as per your examples?)

You can't match the query string part of the URL (everything after the first ?) using a mod_alias RedirectMatch (or Redirect) directive. The RedirectMatch directive matches against the URL-path only. However, this also takes a regex, so . and ? are special characters in the regex.

You need to use mod_rewrite instead with an additional condition that checks the QUERY_STRING server variable.

To redirect /gallery.html?page=<number> (where <number> is any sequence of digits 0-9) then you would need to do something like the following near the top of the .htaccess file (the order of directives is important):

RewriteEngine On

# Redirect to remove "?page=" query string
RewriteCond %{QUERY_STRING} ^page=\d*$
RewriteRule ^gallery\.html$ /$0 [QSD,R=301,L]

The $0 backreference contains the entire match from the RewriteRule pattern (ie. "gallery.html").

The \d* subpattern matches digits 0-9 zero or more times.

The QSD (Query String Discard) flag is necessary to remove the query string from the redirected response.

If there are any other URL parameters in the query string then the redirect will not occur (since the regex won't match).

Test first with 302 (temporary) redirect to avoid potential caching issues.


Aside:

The below weird URLs popped up on Google console...
I did setup the canonical but google is still giving error "Alternative page with proper canonical tag"

I'm not sure exactly where you are seeing that? But that doesn't necessarily look like an "error". (Your rel="canonical" tag appears to be correctly configured.)

A Google site: search on your domain does not return the URLs in question, only /gallery.html, so this redirect is probably not necessary.