Can I pass matched variables to the new URL with mod_rewrite?


Can I pass matched variables to the new URL with mod_rewrite?



I'm quite experienced in PHP but I don't quite use mod_rewrite (although I should). All I want to ask is if it's possible to pass many variables through a single rewrite rule.

For example, is it possible to rewrite this:

localhost/test.php?id=1&name=test&var1=3 

into this:

localhost/mysupertest/ 

and also use the same rewrite rule for different values?

localhost/test.php?id=5&name=another_name&var3=42 

into

localhost/mysupertest/ 

I know it can be done using Ajax, cookie, session, or POST variables, but I really want to use GET variables.




Apache returns sitemap.xml not /sitemap

1:



Rails time stamps on images in CSS
Definitely possible.


PHP cannot find my php files if not under htdocs folder
Something like this would do it:.
mod_rewrite - splitting up arbitary number of terms?
RewriteRule test.php\?(.*)$ mysupertest/ 
However, you will lose your variables if you do that, as it's effectively the same thing as accessing localhost/mysupertest directly, with no query string data.


help needed with Lighttpd and Apache cofiguration with Django
If you want to keep the variables, perhaps in a REST-style url, you can use back-references to rewrite them.


Can apache's ProxyRemote be used to proxy HTTPS requests to mongrel for processing?
As John mentioned, a back-reference is simply a set of brackets, and whatever matches inside them becomes a variable in a numeric ordering scheme..
/usr/bin/env questions regarding shebang line pecularities
RewriteRule test.php\?id=(.*)&name=(.*)&var3=(.*)  mysupertest/$1/$2/$3 
With the above rule, accessing test.php?id=567&name=test&var3=whatever would be the same as accessing mysupertest/567/test/whatever.
How can I parse Apache's error log in PHP?


2:


I'm not exactly sure what you're asking for, but you probably should be using the [QSA] option for mod_rewrite, which will append all the URL parameters from '?' onwards.

For example:.
RewriteRule ^mysupertest/? test.php [QSA] 
Take a look at this question for more details..


3:


Yes, it's possible.

Anything you can match with your regex expression can be rewritten as any url.

Anytime you put part of your regex string inside of parenthesis ( ), it becomes a variable that can be used while rewriting.. You would want something like (I think):.
RewriteRule test.php\?id=[0-9]+&name=.+&var1=[0-9]+ mysupertest/.


4:


The QUERY_STRING is not part of the URI.

From the documentation for RewriteRule:.
Note: Query String. If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively..



98 out of 100 based on 78 user ratings 278 reviews