Tuesday, July 28, 2009

Passing numerous variables with .htaccess mod_rewrite and virtual directories?

Is there a way to use mod_rewrite to rewrite an url from


from:


http://www.example.com/foo/bar/baz/qux/q...


http://www.example.com/foo/bar/baz/qux/q...





to:


http://www.example.com/index.php?a=foo%26amp;b...





I have


Options +FollowSymLinks


RewriteEngine on


RewriteEngine on


RewriteBase /


RewriteRule (.*)/(.*)-(.*)$ index.php?user=$1%26amp;cat=$2%26amp;mode=$3


RewriteRule (.*)/(.*)$ index.php?user=$1%26amp;cat=$2%26amp;mode=$3





which works fine if I only have two variables I want to pass to my script (or three if I don't mind breaking my format slightly). I want to use the forward slash ("/") as the delimiter for variables.





I can potentially process the url uses regex and pathname() in php if I can get apache to redirect to





http://www.example.com/index.php?query=f...


http://www.example.com/index.php?query=f...





(but I would rather use htaccess to rewrite the requests as a GET request).

Passing numerous variables with .htaccess mod_rewrite and virtual directories?
Do you know how many subdirectories you want and what characters are allowed?





I'm guessing if it's 6 subdirectories as in the links above you could use something like:





RewriteEngine on


RewriteRule ^([A-Za-z0-9]+)/


([A-Za-z0-9]+)/


([A-Za-z0-9]+)/


([A-Za-z0-9]+)/


([A-Za-z0-9]+)/


([A-Za-z0-9]+)(/)?$


/test.php?a=$1%26amp;b=$2%26amp;c=$3


%26amp;d=$4%26amp;e=$5%26amp;f=$6





(Without line breaks...just can't seem to keep yahoo answers from deleting the string and putting ...)





This obviously only allows for capital and lowercase letters and numbers in each subdirectory name, but if you create a test.php file with:





%26lt;?php


echo $QUERY_STRING;


?%26gt;





you can see that it breaks up each subdirectory into a variable.


No comments:

Post a Comment