0

How can I make clean URL's with one, two OR three variables?

I want domain.com/mypage to direct to index.php?page=mypage and domain.com/mypage/myproduct to redirect to index.php?page=mypage&var2=myproduct and domain.com/mypage/myproduct/detail to redirect to index.php?page=mypage&var2=myproduct&var3=detail. So some pages have three variables, others have only one or two.

This is the .htaccess I'm using (it includes a part I found at htaccess rewrite pass two variables or one depending if both are available?):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-s 
RewriteRule ^([^/]+)(/([^/]+))? index.php?page=$1&var1=$3 [L,QSA]

It's working for links with one or two variables. How can I make it work for three (or more) variables.

Community
  • 1
  • 1
Rambo
  • 3
  • 1
  • 1

1 Answers1

0

It's probably easiest to just do each of them separately:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?page=$1&var1=$2&var2=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?page=$1&var1=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L,QSA]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Thanks for the fast response. Using your .htaccess breaks everything unfortunately. Except for the homepage, all pages return a 404. For example domain.com works but domain.com/mypage gives a 404. – Rambo Aug 21 '12 at 05:10
  • @Rambo if this isn't in the document root, try removing the `/` in front of `index.php?` – Jon Lin Aug 21 '12 at 05:15
  • Yes, that does the trick. I've even expanded it to take five vars. Thanks so much! – Rambo Aug 21 '12 at 05:21