Apache mod-rewrite and htaccess related Articles

How to remove specific query string from URLs using htaccess


Last Updated on June 8, 2021 by Amit

In this article I will show you how we can remove a specific part of URL using RewriteRule directive in an htaccess file.
This article is based on a StackOverflow question asked by an Apache user. I will explain how you can write RewriteRule to trim a specific part of URL.
Let’s first take the real example from the question asked on StackOverflow.
The question poster wanted to remove /?569/ from their original URL which looked something like .
As you can see the URL part the OP wanted to remove starts with a “question mark” ? so it’s part of “URL query string” .
The entire string after ? is part of query string but as the OP asked, we just have to remove the specific segment.

To trim this query string part , we will write a RewriteRule and a Condition in our our htaccess.

We will use the following condition to check the Query String part in the URL :

RewriteCond %{QUERY_STRING} ^569/(.+)$ [NC]

Since we have to trim only the specific part “?569/” we use the pattern ^569/(.+) that matches a query string starting with 569/ and the (.+) matches other characters and saves the match as we will use it in the target URL.
The RewriteRule we will use here is :

RewriteRule ^lang/part1/$ [R=301,L]

The pattern of the rule matches the URL path which is /lang/part1/ .
In the Rule’s destination you can see the URL is now “” . The %1 is the match from the RewriteCond regex and the ? at the end is there to trim the Old query string so that our final URL could look clean and something like .

The following is the complete Rule to remove the specific part from the mentioned URL :

RewriteEngine on
RewriteCond %{QUERY_STRING} ^569/(.+)$ [NC]
RewriteRule ^lang/part1/$ [R=301,L]

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

By Amit

I am a freelance web developer/designer , blogger and StackOverflow contributer from India.

Leave a Reply

Your email address will not be published. Required fields are marked *