user3452136
user3452136

Reputation: 125

How to redirect url with query string to another url with a query string using .htaccess

First, let me start by saying I'm not very skilled with .htaccess. I did search through StackOverflow and found many redirect examples, but couldn't find one to do what I'm trying to accomplish.

I'm trying to redirect specific URLs with query strings to a different URL with a query string. The query string values are dynamic.

For example, I'd like to redirect this:

https://www.example.com/page/?search=search_string&param1=whatever&param2=whatever

To this:

https://www.example.com/?s=search_string

In the first URL, I don't care about any parameters except for the 'search' value. The value for 'search' in the first URL will be the value of 's' in the second URL. I want to drop all other parameters. Keep in mind that the value for 'search' in the first URL is dynamic.

I tried the following with no luck:

RedirectMatch 301 ^/page/?(.*)$ https://www.example.com/?$1

Upvotes: 0

Views: 1794

Answers (1)

anubhava
anubhava

Reputation: 784958

You will need to use mod_rewrite here to be able to examine and capture values from QUERY_STRING.

RewriteEngine On

RewriteCond %{QUERY_STRING} (?:^|&)search=([^&]+) [NC]
RewriteRule ^page/?$ /?s=%1 [L,NC,R=302]

Upvotes: 1

Related Questions