RaV
RaV

Reputation: 1048

Mod rewrite htaccess - two variables in rewrite rule

I found this solution online:

RewriteEngine On
RewriteBase /test/

RewriteRule ^([^-]*)/$ index.php?page=$1
RewriteRule ^([^-]*)/([^-]*)/$ index.php?page=$1&link=$2 [L]

#dodaje slash na koncu
RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L]

The first one RewriteRule works perfect, it returns me $_GET['page']=130. But when it comes to second one, it returns me $_GET['page']=index.php instead of $_GET['page']=130 and $_GET['link']=35. That finish with SQL error, because of numeric id of page.

Normal link looks like:

?page=136

?page=136&link=35

Rewrited one:

/136/ - works

/136/35/ - doesn't work, $_GET['page']=index.php

Upvotes: 4

Views: 364

Answers (1)

Justin Iurman
Justin Iurman

Reputation: 19016

You can replace your current code by this one (your htaccess has to be in test folder, and it's the same for index.php)

RewriteEngine On
RewriteBase /test/

# add trailing slash if no trailing slash and not an existing file/folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+[^/])$ $1/ [R=301,L]

RewriteRule ^([^/]*)/([^/]*)/$ index.php?page=$1&link=$2 [L]
RewriteRule ^([^/]*)/$ index.php?page=$1 [L]

You can try these links

  • http://domain.com/test/136/35/ internally rewrites to index.php?page=136&link=35
  • http://domain.com/test/136/35 redirects to http://domain.com/test/136/35/
  • http://domain.com/test/136/ internally rewrites to index.php?page=136
  • http://domain.com/test/136 redirects to http://domain.com/test/136/

Upvotes: 3

Related Questions