views:

234

answers:

2

I have these rewriteRules which work fine:

# 4 params
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?app=$1&do=$2&what=$3&id=$4 [NC,L,QSA]

# 3 params
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?app=$1&do=$2&what=$3 [NC,L,QSA]

# 2 params
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?app=$1&do=$2 [NC,L,QSA]

# 1 param
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?app=$1 [NC,L,QSA]

Now, i would like that if the url contains /api/ then i would like it to point to a file called public.api.php but still have those 4 query vars as parameters. I could arrange that via explicitating all the rules in the order in which they should be processed, but it makes for a quite lengthy .htaccess file.

My questions is this: is there a way to add some sort of conditional rule that would check if /api/ is in the requested uri and according to this, point to index.php or public.api.php without repeating the processing of the four query vars?

A: 

Just escape the ? and capture all params as one group?

RewriteRule ^.*/api/.*\?(.*)$ /public.api.php?$1 [NC,L,QSA]
fforw
could you elaborate? I'm not sure i understand the logic you propose.
pixeline
Just copy over every request parameter, all matched in one group, like sketched in that RewriteRule above, which admittedly is not really tested.
fforw
Your regexp all don't escape the ? which means "any character one time" in regexp-ish.
fforw
This won’t work as the URL query is not part of the URL path.
Gumbo
+1  A: 

The easiest would be if you just add some rules like:

RewriteRule ^api/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ public.api.php?app=$1&do=$2&what=$3&id=$4 [NC,L,QSA]
RewriteRule ^api/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ public.api.php?app=$1&do=$2&what=$3 [NC,L,QSA]
RewriteRule ^api/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ public.api.php?app=$1&do=$2 [NC,L,QSA]
RewriteRule ^api/([A-Za-z0-9-]+)/?$ public.api.php?app=$1 [NC,L,QSA]

Edit    Here’s another approach:

RewriteRule (^|.+?/)api/?([^/].*)?$ $1$2

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?app=$1&do=$2&what=$3&id=$4 [NC,L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?app=$1&do=$2&what=$3 [NC,L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?app=$1&do=$2 [NC,L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?app=$1 [NC,L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^?\ ]+/)?api/?
RewriteRule ^index\.php$ public.api.php [L]
Gumbo