Mod_rewrite help & mdash; Convert subdirectories to GET variables

advertisements

I have an app sitting in http://mysite.com/myapp. I want to put an htaccess file in the /myapp folder such that all additional sub-directories get converted to a GET variable. E.g.

http://mysite.com/myapp => /myapp/index.php   # this is happening)
http://mysite.com/myapp/foo => index.php?sub=foo
http://mysite.com/myapp/bar/ => index.php?sub=bar/
http://mysite.com/myapp/foo/bar?baz=bop => index.php?sub=foo/bar&baz=bop

This ain't working:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(/.*)\?(.*) ./index.php?sub=$1&$2 [NC]


Your rewrite rule does not make sense as RewriteRule does not work with query string directly (means -- it's not part of the URL) -- it only works with path part of the URL (e.g. in /foo/bar?baz=bop path part will be /foo/bar). To match query string you have to use RewriteCond.

In your case you do not need to do even that ... as you are not "completely modifying/altering" existing query string .. just adding a bit to it.

Try this code:

Options +FollowSymlinks
RewriteEngine On

RewriteRule ^(?!index\.php)(.*)$ index.php?sub=$1 [QSA,L]

1. QSA tells mod_rewrite to append current query string to the new one.

2. This rule excludes requests to index.php file to prevent infinite redirect loop.

EDIT:

3. This rule (as well as your own attempt) will rewrite requests to any URL including any existing files (e.g. images, css etc). To by-pass this "restriction" (I've wrote it using your example), add these lines just before RewriteRule line:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d