Friday, September 27, 2013

htaccess mod Rewrite Rule - optional parameters

I want to rewrite the below:

  1. http://www.mywebsite/address/12345/ to http://www.mywebsite/address/?param1=12345
  2. http://www.mywebsite/address/12345/12 to http://www.mywebsite/address/?param1=12345&param2=12
  3. http://www.mywebsite/address/12345/?{otherparam}=1 to http://www.mywebsite/address/?param1=12345&{otherparam}=1

Below is what I have in the .htaccess file. I have the first two working fine yet am struggling with the 3rd. I need the third to pass param1 & moreover pass other optional parameters. Can anyone assist?

RewriteRule ^address/([^/.]+)/?$ address/?param1=$1  [NC]RewriteRule ^address/([^/]+)/([^/.]+)/?$ address/?param1=$1&param2=$2  [NC]

You’re looking for the QSA flag, which appends any existing query string to the newly constructed one in the rule’s target:

RewriteRule ^address/([^/.]+)/?$ address/?param1=$1  [NC,QSA,L]RewriteRule ^address/([^/]+)/([^/.]+)/?$ address/?param1=$1&param2=$2  [NC,QSA,L]

No comments:

Post a Comment