Forum Moderators: phranque
Usually I have 1 to 3 parameters, not in any particular order. In this redesign, I can put them in a sequence if need be, but I am not sure how to handle the missing parameters. I thought of always including all six using zero for null as none will ever really be zero.
I would like to get to a more friendly URL like
[mysite.com...]
or better yet
[mysite.com...]
which I would like to translate back to:
[mysite.com...]
I cannot figure out how to this without 6*5*4*3*2*1=720 RewriteRule statements.
is there no way to pass the output of one RewriteRule statement to another RewriteRule?
I tried creating a Perl program to handle this but could not get it to work. Probably because the site is on a name based shared vps with half a dozen other sites. The Apache documents say that the program will be started when the server starts and will handle the rewrites as they come in.
You could also use a series of six RewriteConds testing REQUEST_URI, with each one passing any previously-found variables to the next RewriteCond so as to 'preserve them', followed by a single RewriteRule. This method would parse out the variables in any order (3 variables shown here for example):
RewriteCond %{REQUEST_URI} /b([0-9]{1,4})/?
RewriteCond %1<>{REQUEST_URI} ([^<]*)<>/e([0-9]{1,4})/?
RewriteCond %1<>%2<>%{REQUEST_URI} ([^<]*)<>([<]*)<>/d([0-9]{1,4})/?
RewriteRule -([a-z]+\.html)$ /dodah.php?b=%1&e=%2&d=%3&page=$1 [L]
Jim
After a great deal of discussion here, we have come to a design objective that I am not sure we can meet.
We want the shortest possible pseudo page names; we want to omit unused parameters, not put them in as zeros.
Again there are from zero to six parameters and they are always followed by a page name in the form modelpage.html. (And sometimes modelpage.htm, although we could clean this up and be consistent)
The desired URL would look like this:
[mysite.com...]
This is a great objective. I cannot figure out how to build the output:
[mysite.com...]
I am going insane trying to build these rules and conditions when I may or may not have parameters for a, b, c, d, e, f and, ideally, we would like them to appear in any sequence, except that the page=modelpage.htm will always be last.
In your example above, as soon as a parameter is omitted the rule is aborted. I am back to my original problem of hundreds of permutations.
The following code should work, given that each URL parameter is 'tagged' with a letter (a-f), followed by one to four digits, and then a slash. It will be ungodly-inefficient and slow, but if your team insists on a variable-order, optional-parameter-presence URL, that's what you get, because it makes parsing the URL quite difficult.
# First rule clears any query string on modelpage request
# (needed to prevent possible query string injection exploits)
RewriteRule ^(.*/modelpage\.html?)$ $1?
# Next, check for each of six parameter tags (a-f) and add to query string if present
RewriteRule ^(.*a([0-9]{1,4})/.*modelpage\.html?)$ $1?a=$2 [QSA]
RewriteRule ^(.*b([0-9]{1,4})/.*modelpage\.html?)$ $1?b=$2 [QSA]
RewriteRule ^(.*c([0-9]{1,4})/.*modelpage\.html?)$ $1?c=$2 [QSA]
RewriteRule ^(.*d([0-9]{1,4})/.*modelpage\.html?)$ $1?d=$2 [QSA]
RewriteRule ^(.*e([0-9]{1,4})/.*modelpage\.html?)$ $1?e=$2 [QSA]
RewriteRule ^(.*f([0-9]{1,4})/.*modelpage\.html?)$ $1?f=$2 [QSA]
# Now rewrite modelpage.html to dodah.php, leaving the accumulated query string intact
RewriteRule /modelpage\.html?$ /dodah.php [L]
Jim
RewriteRule /modelpage\.html?$ /dodah.php?page=modelpage.html [QSA,L]
RewriteRule /modelpage\.(html?)$ /dodah.php?page=modelpage.$1 [QSA,L]
Jim