Multiple Variable ReDirects

Discuss practical ways rearrange URLs using mod_rewrite.

Multiple Variable ReDirects

Postby gtlitc » Wed Jul 09, 2008 2:44 am

Code: Select all
# Rewrite static URLs with 0 param to dynamic.
rewriteRule ^cat/?$ /shop.php [L]

# Rewrite static URLs with 1 param to dynamic.
rewriteRule ^cat/([^/]+)/?$ /shop.php?var1=$1 [L]

# Rewrite static URLs with 2 params to dynamic.
rewriteRule ^cat/([^/]+)/([^/]+)/?$ /shop.php?var1=$1&var2=$2 [L]

# Rewrite static URLs with 3 params to dynamic.
rewriteRule ^cat([^/]+)/([^/]+)/([^/]+)/?$ /shop.php?var1=$1&var2=$2&var3=$3 [L]


I have this working well so that the following happens.
website.com/cat/a/b/c --> website.com/shop.php?v1=a&v2=b&v3=c

But really I would like to remove the "cat" pseudodirectory, thus.
website.com/a/b/c --> website.com/shop.php?v1=a&v2=b&v3=c


Any ideas. Ive been round the block on this one! going a bit crazy :=}

gtlitc
 
Posts: 9
Joined: Wed May 30, 2007 2:18 am

Postby richardk » Wed Jul 09, 2008 2:02 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# If it's not a request for an existing file
RewriteCond %{SCRIPT_FILENAME} !-f
# and it's not a request for an existing directory
RewriteCond %{SCRIPT_FILENAME} !-d
# rewrite.
RewriteRule ^([^/]+)(?:/([^/]+)(?:/([^/]+))?)?/?$ /shop.php?var1=$1&var2=$2&var3=$3 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Working but how can I expand this to 7 vars

Postby gtlitc » Thu Jul 10, 2008 1:58 am

Working but how can I expand this to 7 vars?
I have tried to work out how your regexp is working but vant get this to work with 7 vars.

Any explanation most appritiated.

Thanks in advance for your help.

8)
gtlitc
 
Posts: 9
Joined: Wed May 30, 2007 2:18 am

Postby richardk » Sun Jul 13, 2008 10:55 am

Code: Select all
(?:/([^/]+))?

is nested here
Code: Select all
(?:/([^/]+)HERE)?

You can have up to 9 (including the first one).

7
Code: Select all
^([^/]+)(?:/([^/]+)(?:/([^/]+)(?:/([^/]+)(?:/([^/]+)(?:/([^/]+)(?:/([^/]+))?)?)?)?)?)?/?$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 40 guests

cron