RewriteRule for .htaccess file not working

Viewed 130

I have a requirement where I need to redirect users like below : "www.example.com/text1?a=1&b=2" to "www.example.com/text2?a=1&b=2&c=3"

For this, in my .htaccess file I am using the below mod-rewrite rule as :

  RewriteEngine on
  RewriteRule /text1(.*) www.example.com/**text2?c=3 [R=301,L,QSA]

Have also tried :

  RewriteEngine on
  RewriteRule /text1(.*) www.example.com/**text2$1&c=3

However, the above is not working for me and if I attempt to go to "www.example.com/text1?a=1&b=2", I get a straight 404.

Could someone please advise?

Thank you much.

1 Answers

Could you please try following, its mostly a internal redirect so we need not to have R=301 in here.

RewriteEngine ON
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^text1/?$ /text2?%{QUERY_STRING}&c=3 [NC,NE,L]

OR as per anubhava sir's comments you could try following. I will discuss with him more in morning about my above one as it's late night here

RewriteEngine ON
RewriteRule ^text1/?$ /text2?c=3 [NC,QSA,L]

Explanation:

RewriteEngine ON: Making RewriteEngine ON so that rules MOD rewrite rules are allowed to be written in apache(.htaccess file).

RewriteRule:

  • ^text1/?$ On left hand side checking if URI starts from text1 and with optional / then go to right side of this rule and redirect.
  • /text2?%{QUERY_STRING}&c=3 [NC,NE,L]: Creating new value of uri here, it starts from text2 then %{QUERY_STRING} is default variable which has value of your already passed query string(after ?) a=1&b=2 so it adds that old uri's query string, then &c=3 adds new value to this URI here. NOw coming to flags NC,NE,L, L means for this condition its last rule, NC means ignore case applied in URI(left hand side) and NE to NOT to convert values(which you were getting %26 in your comments).
Related