mod_rewrite rule


I have a simple rule to take requests for html pages and pass them through a content management engine.

eg

news.html will be requested by apache as index.php?page=news
using
RewriteRule ^(.*)\.html$ index.php?page=$1 [L]

I now need to get the following working

news.html?a=1&b=2 as index.php?page=news&a=1&b=2

(so that the site works a= and b= will vary from page to page so the a= and b= can't be hard coded within the rule)

Nothing I've done with this gets it to work.
eg these fail
RewriteRule ^(.*)\.html(.*)$ index.php?page=$1&$2 [L]
LVL 3
jglyonsAsked:
Who is Participating?
 
caterham_wwwCommented:
You cannot access the QueryString from within a RewriteRule-pattern.

The case here: Just use the QSA-Flag (QueryString append), which adds the existing queryString to the new one:

RewriteRule ^(.*)\.html$ index.php?page=$1 [QSA,L]
0
 
m1tk4Commented:
RewriteRule ^(.*)\.html(\?(.*))$ index.php?page=$1&$3
0
 
jglyonsAuthor Commented:
m1tk4 Not sure why you've gone to $3 but that rule breaks what worked at the start so that it now returns 404 page not found.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
m1tk4Commented:
$3 because there are three sets of ()'s now. Turn on Rewrite logging and it will show you what's wrong with it.
0
 
m1tk4Commented:
RewriteRule ^(.*)\.html(\??(.*))$ index.php?page=$1&$3 [last]

sorry, missed one ?
0
 
jglyonsAuthor Commented:

caterham_www GENIUS!  3 hours of tearing hair out and it was that simple.  Good answer thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.