Clean URL rewrite to CGI & QUERY based URL

Discuss practical ways rearrange URLs using mod_rewrite.

Clean URL rewrite to CGI & QUERY based URL

Postby w33 » Sat Sep 27, 2003 5:06 pm

I would like to rewrite
/category/subcat/cat/page2/orderd/sort1
to
/cgi-bin/script.cgi?cat=category/subcat&page=2&order=d&sort=1
using Apache Rewrite rule.

Conditions:
1) We not necessarily have Query parameters, so we must treat cases when Query parameters are missing.
2) The Query parameter order is not necessarily always the same order.
3) If needed should be possible to enhance with new Query parameters.
4) I would not use external rewrite rule because of speed reasons

First solution was:
RewriteRule ^(.*)/cat/(page|order|sort)([a-zA-Z0-9]+)/(page|order|sort)([a-zA-Z0-9]+)/(page|order|sort)([a-zA-Z0-9]+) /cgi-bin/script\.cgi\?cat=$1&$2=$3&$4=$5&$6=$7
But this doesn't match the Condition nr.1.

Next was this solution:
RewriteRule ^(.*)/cat/(.*)page([0-9]+)(.*) /cgi-bin/apro/view.cgi?cat=$1&page=$3$2$4
RewriteRule ^(.*)/order(a|d)(.*) $1&order=$2$3
RewriteRule ^(.*)/sort([0-9]+)(.*) $1&sort=$2$3

This result match all conditions, but result has bugs in rewrite_log:
rewrite category/subcategory/cat/page2/orderd/sort1 -> /cgi-bin/script.cgi?cat=category/subcategory&page=2/orderd/sort1
rewrite /cgi-bin/script.cgi/page2/orderd/sort1 -> /cgi-bin/script.cgi/page2&order=d/sort1
rewrite /cgi-bin/script.cgi/page2&order=d/sort1/page2/orderd/sort1 -> /cgi-bin/script.cgi/page2&order=d/sort1/page2/orderd&sort=1

Any idea how could be possible to solve the problem?

Thanks in advance,
W33
w33
 
Posts: 5
Joined: Sat Sep 27, 2003 4:44 pm

Postby Guest » Mon Oct 13, 2003 11:59 pm

HI,

have you found a solution to your problem?

I would need something similar, with exactly the same conditions, but a little bit different scenario:

Orig: www.foobar.com/(XXXXX)/somewhere?x=y
Rewrited to: www.foobar.com/somewhere?x=y&referenceID=XXXXXX

Basically, anything in brackets in the middle of the URL goes as a query string at the end. The only "problem" would be that it is not necessary there will always be already existing quersy string so I must use either ? or & respectivly.

Oh yes, one more thing: the user MUST ALWAYS see just the original, unmoded URL.

Any ideas?

Regards.
Guest
 

Solution

Postby w33 » Tue Oct 14, 2003 4:20 am

Hi,

My final solution was this kind of short URL:
http://site/category/subcat/?show_p5_s2_oa
I choosed this because it is very user friendly, and has several advantages.

It should rewritten to this URL:
http://site/cgi-bin/script.cgi?cat=cate ... r=d&sort=1

Solution:
RewriteCond %{HTTP_HOST} ^subdomain\.site$
RewriteCond %{QUERY_STRING} ^view_p(.*)_s(.*)_o(.)$
RewriteRule ^(.*)/$ /cgi-bin/script.cgi?cat=$1&page=%1&sort=%2&order=%3 [L]

These rewrites are internal rewrites, so user always sees the short URL.
It works fine, however doesn't fit the 2) condition.
2) condition could be easily implemented by dividing the query matching into multiple parts, similar way as the first solution did.

I hope this will be helpful for you.

Regards,
W33
w33
 
Posts: 5
Joined: Sat Sep 27, 2003 4:44 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 20 guests

cron