user3667960 user3667960 - 15 days ago 6
PHP Question

PHP htaccess rewrite rule to same page with different parameters

I'm using the following

htaccess
rules. The first one is working fine. The second rule is not working. The rule I have written for the second one is taking to the first one.

Options +FollowSymLinks
RewriteEngine On
RewriteBase /Burjalif/

RewriteCond %{THE_REQUEST} /Products(?:\.php)?\?MC=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]

RewriteCond %{THE_REQUEST} /Products(?:\.php)?\?MC=([^&]+)&SC=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L]

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# PHP hiding rule
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]

RewriteRule ^([^/]+)$ Products.php?MC=$1 [L,NC,QSA]

RewriteRule ^([^/]+)/([^/]+)$ Products.php?MC=$1&SC=$2 [L,NC,QSA]


Please help me to fix this.

Answer

Change your first rule to this:

RewriteCond %{THE_REQUEST} /Products(?:\.php)?\?MC=([^\s&]+)\sHTTP [NC]
RewriteRule ^ %1? [R=302,NE,L]

This will ensure that you're matching only one parameter in query string.