3

I am trying to create a mod_rewrite that when a user enters, or it shows in the URL:

www.example.com/var1/var2/var3/var4/

it outputs the same as

www.example.com?page=var1&cat=var2&subcat=var3&subsubcat=var4

but if there is a change in the number variables, it will still work, so the variables could be:

www.example.com/var1/
or
www.example.com/var1/var2/
or
www.example.com/var1/var2/var3/
or
www.example.com/var1/var2/var3/var4/

If I use the following, it only works for www.example.com/var1/var2/var3/var4/ but not the others:

Options +FollowSymLinks RewriteEngine On RewriteBase /

RewriteRule ^([^&]+)/([^&]*)/([^&]*)/([^&]*)/$ /?page=$1&cat=$2&subcat=$3&subsubcat=$4 [L]

If I use the following code using CHAINING, it works as a chain to an extent, but the problem is that for

www.example.com/var1/var2/
www.example.com/var1/var2/var3/
www.example.com/var1/var2/var3/var4/

it outputs the VAR1 as "/", so it is ignoring the first variable using the following code:

Options +FollowSymLinks
RewriteEngine  On
RewriteBase /

RewriteRule ^([^&]+)/$ /?page=$1 [C]
RewriteRule ^([^&]+)/([^&]*)/$ /?page=$1&cat=$2 [C]
RewriteRule ^([^&]+)/([^&]*)/([^&]*)/$ /?page=$1&cat=$2&subcat=$3 [C]
RewriteRule ^([^&]+)/([^&]*)/([^&]*)/([^&]*)/$ /?page=$1&cat=$2&subcat=$3&subsubcat=$4 [L]

The order is always the same so this should be easy. How can I make this work please?

anubhava
  • 761,203
  • 64
  • 569
  • 643
R A
  • 119
  • 1
  • 12

1 Answers1

2

Probably I am missing something but IMO you don't need chaining C flag here. Please consider these rules:

RewriteEngine on
Options +FollowSymlinks -MultiViews

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/?$ /?page=$1 [L]

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/([^/]*)/?$ /?page=$1&cat=$2 [L]

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/([^/]*)/([^/]*)/?$ /?page=$1&cat=$2&subcat=$3 [L]

RewriteCond  %{QUERY_STRING} !^page= [NC]
RewriteRule ^([^/]+)/([^/]*)/([^/]*)/([^/]*)/?$ /?page=$1&cat=$2&subcat=$3&subsubcat=$4 [L]

I added RewriteCond %{QUERY_STRING} !^page= [NC] in all rules to make sure there is infinite looping. Also I made trailing slash optional by adding ? in the end of each rule.

All of above rules are working fine in my testing.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thank you. I require an amendment though. I need it to work with different field options for var2, var3 and var4. To be able to make an if-or-else for the choice of what field is being used for what variable and to tell the difference. For example, it is easy to distinguish the fields for each variable here: www.example.com?page=var1&cat=var2&subcat=var3&subsubcat=var4 www.example.com?othervar1=var1&othervar2=var2&othervar3=var3&othervar4=var4 but for: www.example.com/var1/var2/var3/var4/ this string requires the ability to distinguish what field the variable is for with mod_rewrite. – R A Apr 25 '11 at 07:39
  • Also, can you tell me what the "-MultiViews" bit means at the beginning as I have not come across this before? – R A Apr 25 '11 at 07:40
  • I have actually posted this as another question as I was unable to reply before. To go to this new question, please click this [link](http://stackoverflow.com/questions/5776208/mod-rewrite-for-url-variables-removing-empty-parameters-and-using-more-than-1fie). Thanks. – R A Apr 25 '11 at 07:42
  • Sure I will go to other question and answer there. Meanwhile would you please mark this answer as posted since this answer is for the question posted here and works fine for this particular question. – anubhava Apr 25 '11 at 11:46