Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have this rule:

RewriteEngine On
RewriteRule   ^/page/([a-zA-Z0-9]+)\?(.*)$    /index.php?page=$1&$2

It should take something like this:

/page/contact?cake=lie

and go to the page:

/index.php?page=contact&cake=lie

I've tested this with tools like RegExr and RegexPal, and it works there. But when putting it into Apaches config, visiting the url gives Apaches 404 page.

Can anyone help?

share|improve this question

4 Answers 4

up vote 2 down vote accepted

If you want to pass the existing query string verbatim, mod_rewrite provides the QSA flag for this purpose. Using it, your rule set could be simplified to the following:

RewriteEngine On
RewriteRule   ^/page/([a-zA-Z0-9]+)$    /index.php?page=$1 [QSA]

mod_rewrite will then automatically append any existing query string to the one that you've added via your replacement. The result is effectively the same as Steven Monai's approach, but I feel this method a bit cleaner.

share|improve this answer

By default mod_write matches against REQUEST_URI, which only contains the URI but not the dynamic parameters, so it will discard everything after question mark.

To match against full URI, you need to use THE_REQUEST instead (or if there's another method , please enlighten me!). Please note that it contains the full request the client sent, for example "GET /foo?bar=baz HTTP/1.1".

Try something like

RewriteCond %{THE_REQUEST} ^(.*)$
RewriteRule ^GET /page/([a-zA-Z0-9]+)\?(.*) HTTP.*$    /index.php?page=$1&$2
share|improve this answer
    
So this would be the only way to keep the GET parameters on a rewrite? –  borntyping Nov 12 '10 at 11:48
    
As far as I know, yes. But I might be wrong, too, since mod_rewrite truly is damned voodoo and its flexibility amazes me every time. :-) –  Janne Pikkarainen Nov 12 '10 at 11:50
    
Ok, thanks. Any idea how I could get the parts of THE_REQUEST that I need? –  borntyping Nov 12 '10 at 12:19
    
It's late Friday afternoon and my brain activity is nearing zero, but I edited my reply. Try something like that. –  Janne Pikkarainen Nov 12 '10 at 12:24
    
You can also access the query string with QUERY_STRING. –  Sam Halicke Nov 12 '10 at 15:42

Do you have rewrite engine turned on?

RewriteEngine On
share|improve this answer
    
Yes. Other rewrite rules are working, but are disabled while I make this work. –  borntyping Nov 12 '10 at 11:22

Janne's assessment of why your rule doesn't work is correct. But I would take a less complicated approach to solve it:

RewriteRule ^/page/([a-zA-Z0-9]+)$ /index.php?page=$1&%{QUERY_STRING}

There's no need to pick apart the raw request to get at the query string when Apache already gives it to you in %{QUERY_STRING}.

The list of server variables available to you via the %{VARNAME} syntax is documented here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.