user3224207
user3224207

Reputation: 437

URL redirect in htaccess file

I want to ask how to redirect in htaccess file from same url with query string parameters to same url without query string parameters. For example if my url is www.example.com/abc.php?cid=5&scid=10, I want to redirect it to www.example.com/abc.php

Please note that the url is same, just query string parameters have been removed in redirection. I have written htaccess file code for urls with single query string but with more than one parameters it is not redirecting.

Here is my code for single parameter which is working as expected:

RewriteCond %{QUERY_STRING} cid=7$
RewriteRule (.*) /contact-us.php? [R=301,L]

It redirects contact-us.php?cid=7 page to contact-us.php

But when I'm exteding the code for multiple parameters it doesn't redirect. Here is what I have written in this case:

RewriteCond %{QUERY_STRING} cid=9$
RewriteCond %{QUERY_STRING} scid=14$
RewriteRule (.*) /stakeholders.php? [R=301,L]

This one is supposed to redirect stakeholders.php?cid=9&scid=14 page to stakeholders.php page. Can someone tell me what is wrong in second code?

I have also googled it but almost all the results are about redirecting from one url to another url not to same url with query string parameters removed. Thanks.

Upvotes: 1

Views: 825

Answers (1)

Joe
Joe

Reputation: 4897

You need to connect your query into one condition, like this:

RewriteEngine On
RewriteCond %{QUERY_STRING} cid=9&scid=14 [NC]
RewriteRule ^(.*)$ http://www.example.com/stakeholders.php? [R=301,NC,L]

This will work for the specific query of cid=9&scid=14, if you wanted to do it for any query then you could use cid=(.+)&scid=(.+) instead.

The ? on the end of the rewritten URL stops the original query from appearing on the end of it and it uses a 301 redirection to perform the change.

Upvotes: 1

Related Questions