Discussion:
Use mod_rewrite to fetch parameters in random order
(too old to reply)
Álvaro G. Vicario
2012-03-27 10:44:41 UTC
Permalink
Given a site that has URLs like these:

http://example.com/catalogue
http://example.com/catalogue/category-20
http://example.com/catalogue/customer-5
http://example.com/catalogue/category-20/customer-5
http://example.com/catalogue/customer-5/category-20

... is there a sensible Apache way to make internal redirects to:

http://example.com/catalogue.php
http://example.com/catalogue.php?category_id=20
http://example.com/catalogue.php?customer_id=5
http://example.com/catalogue.php?category_id=20&customer_id=5
http://example.com/catalogue.php?customer_id=5&category_id=20

...?

My attempts are basically getting me nowhere:

RewriteRule ^(.*/)category-(\d+)(.*)$ $1$3?category_id=$2 [QSA]
RewriteRule ^(.*/)customer-(\d+)(.*)$ $1$3?customer_id=$2 [QSA]
RewriteRule ^catalogue/?$ catalogue.php [L,QSA]
# Request exceeded the limit of 10 internal redirects due to probable
# configuration error. Use 'LimitInternalRecursion' to increase the
# limit if necessary

RewriteRule ^(.*/)category-(\d+)(.*)$ $1$3?category_id=$2 [QSA]
RewriteRule ^(.*/)customer-(\d+)(.*)$ $1$3?customer_id=$2 [QSA]
RewriteRule ^catalogue/? catalogue.php [L,QSA]
# Matches everything that starts with /catalogue (not intended)

It'd though it'd be a common task but I've been totally out of luck in
Google.
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Kees Nuyt
2012-03-27 17:15:23 UTC
Permalink
On Tue, 27 Mar 2012 12:44:41 +0200, "Álvaro G. Vicario"
Post by Álvaro G. Vicario
http://example.com/catalogue
http://example.com/catalogue/category-20
http://example.com/catalogue/customer-5
http://example.com/catalogue/category-20/customer-5
http://example.com/catalogue/customer-5/category-20
http://example.com/catalogue.php
http://example.com/catalogue.php?category_id=20
http://example.com/catalogue.php?customer_id=5
http://example.com/catalogue.php?category_id=20&customer_id=5
http://example.com/catalogue.php?customer_id=5&category_id=20
...?
RewriteRule ^(.*/)category-(\d+)(.*)$ $1$3?category_id=$2 [QSA]
RewriteRule ^(.*/)customer-(\d+)(.*)$ $1$3?customer_id=$2 [QSA]
RewriteRule ^catalogue/?$ catalogue.php [L,QSA]
# Request exceeded the limit of 10 internal redirects due to probable
# configuration error. Use 'LimitInternalRecursion' to increase the
# limit if necessary
RewriteRule ^(.*/)category-(\d+)(.*)$ $1$3?category_id=$2 [QSA]
RewriteRule ^(.*/)customer-(\d+)(.*)$ $1$3?customer_id=$2 [QSA]
RewriteRule ^catalogue/? catalogue.php [L,QSA]
# Matches everything that starts with /catalogue (not intended)
It'd though it'd be a common task but I've been totally out of luck in
Google.
It's easier using php:

<VirtualHost *:80>
ServerName example.com
ServerAdmin ***@example.invalid
DocumentRoot "/var/www/ctlg"
# catalogue is the PHP script
AliasMatch ^/(.*) "/var/www/ctlg/catalogue/$1"
<Directory "/var/www/ctlg">
AllowOverride None
Order deny,allow
Deny from all
Allow from all
<FilesMatch "^.*$>
ForceType application/x-httpd-php
</FilesMatch>
</Directory>
</VirtualHost>

The PHP script evaluates everything behind
http://example.com/catalogue


It evaluates
$_SERVER['PATH_INFO']
and
$_SERVER['QUERY_STRING']
(ok, not the latter in your use case)

and acts upon them.

HTH

Best regards,
--
( Kees Nuyt
)
c[_]
Loading...