Gremlin
Gremlin

Reputation: 29

Remove query string from the begging of a url

I've got a buggy query string in my url so it appears in the beginning of a url: https://website.com/?lang=en/wp-content/uploads/2019/10/image.png How can I remove this query string so that the url redirects to https://website.com/wp-content/uploads/2019/10/image.png? I've tried the following rules to no avail:

RewriteEngine On
RewriteCond %{QUERY_STRING} "lang=" [NC]
RewriteRule ^(.*)  /$1? [R=301,L]

Any help appreciated. Thank you!

Upvotes: 0

Views: 34

Answers (1)

04FS
04FS

Reputation: 5820

The path component of your URL is empty here, so RewriteRule ^(.*) will only capture an empty string, and that means $1 will be empty as well.

The info you are looking for is in the query string - so you have to capture it from there:

RewriteEngine On
RewriteCond %{QUERY_STRING} lang=(.*) [NC]
RewriteRule . /%1? [R=301,L]

%1 instead of $1, because that is a back reference not to the RewriteRule pattern, but to the Condition.

Now this might lead to unexpected results if your query string could ever contain more GET parameters after lang. In that case, you might have to be a bit more specific with your pattern (like try to match anything after lang, that is not an ampersand - lang=([^&]*))

Upvotes: 1

Related Questions