
If you have been using a content management system like Q2A that creates clean URLs by removing query strings, then you might encounter challenges redirecting the URL to another website because the query string parameters will still be appended to the target URL.
How to remove query strings from URLs with numbers
To remove query strings from a redirected URL with a specific number (for example 703), then add the code below to your .htaccess file in Apache. Your server must be using at least Apache 2.4 and the root domain should be the same.
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} page-query=703 [NC]
RewriteRule (.*) /example-url-of the-page/ [R=301,L,QSD]
</IfModule>
How the Code Works
In the example above, the query string is page-query=703 and the target URL is https://example.com/example-url-of the-page/.
The directive above tells mod_rewrite to replace everything after the query string page-query=703 with /example-url-of the-page/.
QSD is used to remove any query strings left after the target URL.
Redirecting multiple URLs with the same query string
If you want to redirect multiple URLs with random numbers to one URL then use the code below
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} page-query= [NC]
RewriteRule (.*) /example-url-of the-page/ [R=301,L,QSD]
</IfModule>
If you are redirecting URLs with different numbers to different URLs then you will have to manually add the RewriteCond and RewriteRule of the old and new URLs.
Leave a Reply