badsyntax badsyntax - 4 months ago 36
PHP Question

pretty urls with multiple queries and a php bootstrap

Hello I am having trouble getting url queries through my bootstrap and displaying a page. So I'd like to take

site.com?page=user-verify&username=name&hash=12345


and get

site.com/user-verify/name/12345


First I route all requests through my index.php with htaccess like so

Options +FollowSymLinks -MultiViews -Indexes

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /admin/index.php?page=$1&username=$2&hash=$3 [QSA]


Then my index looks at the query and gets the page

if (isset($_GET['page']))
{
$query = $_GET['page'];
}
elseif (!isset($_GET['page']))
{
header('location:home');
}

switch ($query)
{
case 'home':
drawPage(
'home.php',
'Admin Control Panel',
'This is the homepage description'
);
break;
case 'register':
drawPage(
'register.php',
'Register Page',
'This is the register page description'
);
break;
case 'user-verify':
drawPage(
'user-verify.php',
'Verify User',
'This is the register page description'
);
break;
default:
var_dump($query);
break;


When I add &username=name the url becomes just user-verify. If I add /name the url becomes user-verify/name.

How can add more queries after my page query and get them to be pretty like the example above?

Answer

replace this line

RewriteRule ^(.*)$ /admin/index.php?page=$1&username=$2&hash=$3 [QSA]

by this line

RewriteRule ^(.*)/(.*)/(.*)$ /admin/index.php?page=$1&username=$2&hash=$3 [QSA]
Comments