Romaan Naeem
Romaan Naeem

Reputation: 1

How to replace the first parameter in a query string?

I'm trying to change this query "https://myurl.com/puff/?search_keyword=xyz" into this one "https://myurl.com/puff/?s=xyz".

I saw an answer similar to this, but I haven't used regular expressions before and had issues customizing it to what I need. I'm using WordPress and editing the .htaccess file to achieve this.

I looked into How to replace the first parameter name in query string using .htaccess? and tried to use that to customize my own .htaccess but had issues implementing it on my own.

This is what my current .htaccess file looks like:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /puffadvisor/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /puffadvisor/index.php [L]x
</IfModule>

# END WordPress

All help is greatly appreciated :) thank you so much.

Upvotes: 0

Views: 61

Answers (1)

Pinke Helga
Pinke Helga

Reputation: 6682

You need a condition based on %QUERY_STRING and then references to the capture groups prefixed by %

RewriteEngine On
RewriteCond %{QUERY_STRING} (.*(?:^|&))seach_keyword((?:$|=|&).*)
RewriteRule /puff /puff?%1s%2

This {DOCUMENT_ROOT}/.htaccess replaces the parameter name search_keyword with s on /puff location, no matter at which position the parameter occurs.

You might want to rewrite all pathes

RewriteRule .* $0?%1s%2

Upvotes: 1

Related Questions