1

My htaccess rule is :

RewriteCond %{QUERY_STRING} ^param1=val1
RewriteRule ^index\.php$ /page1 [L,R=301]

This redirects to the url : http://myhost/page1?param1=val1

If my url is : http://myhost/index.php?param1=val1&param2=val2

should redirects to : http://myhost/page1?param2=val2

That means, it should not include the matched query string in RewriteCond

2 Answers 2

1

Just use a group matching:

RewriteCond %{QUERY_STRING} ^param1=val1(?:&(.+))?$
RewriteRule ^index\.php$ /page1?%1 [R=301,L,NC]
Sign up to request clarification or add additional context in comments.

4 Comments

Or ^param1=val1(?:&(.+))?$ without the &.
Its working if it has the params. For e.g., If my url is : http://myhost/index.php?param1=val1&param2=val2 should redirects to : http://myhost/page1?param2=val2. This is working fine. But if I has the url : http://myhost/index.php?param1=val1 then it is getting redirected to http://myhost/page1?%1 I want to get rid of that ?%1 part if it does not has the parameters
@Govind Try using a B flag with the R=301 one. If that doesn't help, you'd have to break the rules into two separate ones.
@hjpotter92 : It is not working with B flag. So I have achieved that with 2 rules.. Thanks for your help
0

You can use:

RewriteCond %{QUERY_STRING} .*&?(param\d=val\d)
RewriteRule ^index\.php$ /page1?%1 [R=301,L,NC]

With:
http://myhost/index.php?param1=val1 -> http://myhost/page1?param1=val1
and
http://myhost/index.php?param1=val1&param2=val2 -> http://myhost/page1?param2=val2
But only if the real name is param1, param2, param3...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.