Reputation: 725
Whenever I add an extra $_GET[]
variable into the url and then rewrite it with HTACCESS, PHP seems to get confused and assigns 1 variable (in this case $r) to entire rest of the url string
For example: mydomain.com/index/r/policy/cookiepolicy
if I echo $r
in index.php I get policy/cookiepolicy
, it should only be policy
. Instead of loading the policy.php page, it reverts to the default case (because it contains two GET
variables and cannot find the correct match) and brings me back to the home page. However if I use the "ugly" URL like this: mydomain.com/index.php?r=policy&tab=cookiepolicy
, everything seems to work fine
htaccess
RewriteRule ^index/r/(.*)$ /index.php?r=$1
RewriteRule ^index/r/(.*)/(.*)$ /index.php?r=$1&tab=$2
policy.php
$k = $_GET["tab"];
//I'm loading the tabs using if() statements
if($k == "cookiepolicy"){
...
}
index.php
<?php
$r = $_GET["r"];
if (isset($r) && !empty($r)) {
switch ($r) {
//...(more cases here)
//Why do the tabs not load for the policy page?
case "policy";
include("pages/policy.php");
break;
//...(more cases here)
default:
include("pages/home.php");
break;
}
} else {
include("pages/home.php");
}
?>
Upvotes: 0
Views: 38
Reputation: 8482
The problem is that, with your rewrite rules, the first one in the list is:
RewriteRule ^index/r/(.*)$ /index.php?r=$1
Because that matches the /
character $_GET['r']
is being assigned the value policy/cookiepolicy
.
You can either exclude the /
from the match by using ([^/]*)
instead of (.*)
or simply reverse the order of the rules so that the second match is evaluated before the first:
RewriteRule ^index/r/(.*)/(.*)$ /index.php?r=$1&tab=$2 [L]
RewriteRule ^index/r/(.*)$ /index.php?r=$1 [L]
That way /policy/cookiepolicy
is caught by the first match and:
$_GET['r'] = 'policy'
$_GET['tab'] = 'cookiepolicy'
Upvotes: 1
Reputation: 786291
Have your rules like this:
RewriteRule ^index/r/([^/]+)/([^/]+)/?$ index.php?r=$1&tab=$2 [L,QSA]
RewriteRule ^index/r/([^/]+)/?$ index.php?r=$1 [L,QSA]
Upvotes: 1