Home > Back-end >  Redirect rule to remove query string from URL
Redirect rule to remove query string from URL

Time:04-04

Have been trying to write a redirect rule with query string but did not succeed.

I have the URL example.com/blog/?page=1 or example.com/blog/?hello, so it does not really matter what goes in the query string. How do I write a Redirect rule, so that it cuts the query string and redirects to the URL before the query string. For example, both of those URLs have to redirect to example.com/blog/ so that URL does not contain any query string.

I was trying RewriteRule ^blog/?$ blog/ [R=301,L,NE] but got redirected to 404 page.

Also tried RewriteRule ^blog/?$ /blog/ [R=301,L,NE] and got the message that page is not working, 'URL' redirected you too many times.

BTW, technology I am using is Gatsby with htaccess plugin.

CodePudding user response:

To remove the query string you first need to check that there is a query string to remove, otherwise, it should do nothing.

For example, to remove the query string from /blog/?<query-string> you would do something like this:

RewriteCond %{QUERY_STRING} .
RewriteRule ^(blog)/?$ /$1/ [QSD,R=302,L]

This matches the URL-path blog/ (trailing slash optional) and redirects to /blog/ (with a trailing slash). Your example URL includes the trailing slash, but your regex appears to suggest the trailing slash is optional?

The preceding condition (RewriteCond directive) checks the QUERY_STRING server variable to make sure this is non-empty (ie. it matches a single character, denoted by the dot).

The $1 backreference in the substitution string contains the value from the captured group in the preceding RewriteRule pattern. ie. "blog" in this example. This simply saves repetition. You could just as easily write RewriteRule ^blog/?$ /blog/ [QSD,R,L] instead.

The QSD (Query String Discard) flag removes the original query string from the redirected response, otherwise, this would be passed through by default (which would create a redirect-loop).

If the request does not contain a query string then this rule does nothing (since the condition will fail).

If this is intended to be permanent then change the 302 (temporary) redirect to 301 (permanent), but only once you have confirmed this works as intended. 301s are cached persistently by the browser so can make testing problematic.

was trying RewriteRule ^blog/?$ blog/ [R=301,L,NE] but got redirected to 404 page.

By default, the relative substitution string (ie. blog/) is seen as relative to the directory that contains the .htaccess file and this "directory-prefix" is then prefixed back to the relative URL, so this will (by default) result in a malformed redirect of the form https://example.com/path/to/public_html/blog/.

Also tried RewriteRule ^blog/?$ /blog/ [R=301,L,NE] and got message that page is not working, 'url' redirected you too many times.

This is not checking for (or removing) the query string so this is basically just redirecting to itself - an endless redirect-loop.

  • Related