.htaccess Redirect Only Specific UTF-8 SubDomain U. R. L.s to Its SubDomain

Viewed 24

I have:

https://Restoran.Grupacija.com/?ćumur

Which needs to be redirected to:

https://Restoran.Grupacija.com/

This directive is in .htaccess file in the Root Folder, but it does not work:

Redirect 301 "^/%3Fćumur" https://Restoran.Grupacija.com

I have also tried this, but it does not work either:

Redirect 301 "^/%3F%C4%87umur" https://Restoran.Grupacija.com

My .htaccess file is UTF-8 already.

1 Answers

The mod_alias Redirect directive matches the URL-path only, which notably excludes the query string. (However, neither does the Redirect directive match using a regex, it uses simple prefix-matching instead.)

To match the query string you need to use mod_rewrite instead and match against the QUERY_STRING server variable in a RewriteCond directive.

Note that the browser/user-agent will URL encode the ć as %C4%87 in the HTTP request and the QUERY_STRING server variable is not %-decoded, so we need to match the encoded request. Strictly speaking, both %C4 and %c4 are valid encodings.

I'm assuming you don't need to explicitly check the requested hostname, unless this query string could be valid on another hostname that your server receives?

For example, to perform the redirect as requested, the following would need to go near the top of your .htaccess file before any existing rewrites.

RewriteEngine On

RewriteCond %{QUERY_STRING} ^%[cC]4%87umur$
RewriteRule ^$ / [QSD,R=301,L]

The QSD (Query String Discard) flag removes the query string from the redirect response.

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

(You do not need to repeat the RewriteEngine directive if this already occurs in your .htaccess file.)

Related