Mobile app version of vmapp.org
Login or Join
Harper822

: Crafting a regex for mod_rewrite with more than nine parameters Say for example, I have a URL in this format: http://example.com/(mobile-or-desktop-flag)/(major-venue-category)/(smaller-venue-category)/(venue-name)/(month)-(day)-(year)/

@Harper822

Posted in: #ModRewrite

Say for example, I have a URL in this format:
example.com/(mobile-or-desktop-flag)/(major-venue-category)/(smaller-venue-category)/(venue-name)/(month)-(day)-(year)/(picture-number)/(user-action-on-picture)/(request-ajax-or-not)

where the text in parenthesis represent the parameter data.

and some example URLs are:
example.com/desktop/big/nightclubs/bloke/jan-1-2015/1/download/no-ajax http://example.com/desktop/small/lounges/oko-blu/feb-2-2014/6/share/no-ajax example.com/mobile/tiny/bars/honest-lawyer/mar-3-2013/9/report/ajax

With mod_rewrite, I know I can use through for the first nine parameters, but here, I have 10 parameters, and is considered first parameter with a zero added to it. See RewriteCond at httpd.apache.org/docs/current/mod/mod_rewrite.html

I'm just wondering, is there any way one can include a variable 10th parameter in a rewrite rule, or will I have to merge two parameters into one and extract them into two via a server-side script?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Harper822

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ann8826881

is there any way one can include a variable 10th parameter in a rewrite rule


As far as I'm aware you can only use the backreferences [CO].. ( to references the parenthesised/captured groups and [CO] references the entire pattern that is matched) in Apache regex (eg. mod_rewrite, etc) to reference captured groups in the RewriteRule pattern (%0..%9 for the last matched CondPattern).

Workarounds:


Like you suggest, you could merge two or more parameters into one and let your server-side script parse the URL. The date (month-day-year) is the obvious one. You could simply use mod_rewrite to validate the URL and let your server-side script parse the entire URL into its component parts (a front-controller).
As an extension to the above. You could merge two or more params and rewrite the URL a second time, combining the params into the correct format required by the script you are rewriting to. For example, take the month-day-year parameter(s). If the script you were rewriting to required the date in universal date format (year-month-day) then you could first capture and convert this into one parameter (together with the remainder of the URL).
For example:

RewriteRule ^(w+/[w-]+/[w-]+/[w-]+)/([a-z]{3})-(d{1,2})-(d{4})/(.*) //--/
RewriteRule ^(desktop|mobile)/([w-]+)/([w-]+)/([w-]+)/(d{4}-[a-z]{3}-d{1,2})/(d{1,3})/(download|share|report)/(no-ajax|ajax) /script.php?platform=&majorcat=&minorcat=&vname=&date=&picnum=&action=&ajax= [L]

Avoid capturing groups for any groups you don't need in the current rewrite. ie. Make it a non-capturing group by prefixing with ?:, for example: (?:desktop|mobile).

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme