Tricky mod_rewrite rule - please help


LAMP server, php/Mysql site with mod_rewrite set up and working correctly. The main rewrite rule is this:

RewriteRule ^([^\\.]+)$ /page.php?page=$1 [L]

The reason is that the client wants to have pages with no suffix. This works fine as the suffix is added once php receives the $_GET[‘page’] variable.

The client uses various links to the site for marketing and wants to use certain pages as landing pages. These pages need 5 GET vars so I have the rule:

RewriteRule ^([a-zA-Z-]+)\\?a=/([0-9]+)&b=([0-9]+)&c=([0-9]+)&d=([0-9]+)&e=([0-9]+)$ /page.php?page=$1&a=$2&b=$3&c=$4&d=$5&e=$6 [L]

Above the main rule for these landing pages but it is only passing the first var to the page.php page. I have tried combinations but just can’t get it to work. I think it is to do with the backslashed ?

There may be a better way to get the page name as the first var and then the other vars passed to the php page.

Any help or pointers to tutorials (I have read all the recommended ones and many more) will be much appreciated.

Oops, look like I missed this one :blush:

Anyway, the problem you’re having is that RewriteRules do not work on the query string. You need RewriteCond for that.

In your case it would be something like

RewriteCond %{QUERY_STRING} a=([0-9]+)&b=([0-9]+)&c=([0-9]+)&d=([0-9]+)&e=([0-9]+)
RewriteRule ^([a-zA-Z-]+)$ /page.php?page=$1&a=%1&b=%2&c=%3&d=%4&e=%5 [L]

Or you could simply use Query String Append

RewriteRule ^([a-zA-Z-]+)$ /page.php?page=$1 [L,QSA]

That will append any query string that was originally requested to the new query string :slight_smile: