htaccess hide MVC model parameters

Asked

Viewed 566 times

4

I have the following URL:

http://192.168.1.67/plays/mvc/index.php?route=profile&user=mikas.28

On which route = PAGE and user = USERNAME.USERID

I have the following htaccess

Options -Multiviews
RewriteEngine On

RewriteBase /plays/mvc

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.+)$ index.php?route=$1 [L]

And what I got was:

http://192.168.1.67/plays/mvc/profile&user=mikas.28

Worked to eliminate index.php?route=, I tried to add other Rules that fit to eliminate the others but could not.

My goal is to achieve http://192.168.1.67/plays/mvc/profile/mikas.28

If this is achieved I have to change something in my index.php?

$router = new Router();

if(isset($_GET['route'])) {
   $route = $_GET['route'];
}
else {
   $route = 'home';
}


if(!is_null($router->get($route))) {

   $r = $router->get($route);
   $controllerName = $r['controller'];
   $methodName = $r['method'];

   require_once "controllers/" .$controllerName. '.php';
   $controller = new $controllerName();
   $controller->$methodName();

}

else {
   echo "404 Not Found!";
}
  • I’ve had this kind of problem but I couldn’t solve, hopefully someone will answer us!! Excellent question.

  • Thank you, I always go around with htaccess

  • @Kaduamaral managed to adapt this response to my case. Thank you

2 answers

2

The rewrite rule is misspelled. Instead of:

RewriteRule ^(.+)$ index.php?route=$1 [L]

Write:

RewriteRule ^([^/]+)(/)([^/]+)$ index.php?route=$1&user=$3 [L]

0


I got:

htaccess:

RewriteRule ^(.*)$ index.php?route=$1 [L]
RewriteRule ^([^/]+)(/)([^/]+)$ index.php?route=$1&user=$3 [L]

index php.:

$router = new Router();

if(isset($_GET['route'])) {
   $route = $_GET['route'];
   $route = explode("/", $route)[0];
}
else {
   $route = 'home';
}
...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.