How to configure . htaccess?

Asked

Viewed 145 times

1

I am doing a project for learning in MVC, and I came across a small problem, my . htaccess seems not to be configured properly.

I have the following structure:

  • app
  • public
  • .htaccess

In this htaccess, I have the following code:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^$ public/ [L]
  RewriteRule (.*) public/$1 [L]
</IfModule>

And in the . htaccess I have inside the "public" folder, it’s like this:

<IfModule mod_rewrite.c>
   Options -Multiviews
   RewriteEngine On
   RewriteBase /teste/public
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

The problem is when I access the "home" page of my site, because it accesses so: http://localhost/test/public, and not just http://localhost/test/, how do you imagine it would be right.

When I tried to put it on the online server (webhostapp), it also accessed with "public" at the end, and only worked when I typed "https://", otherwise it would go to the page 'not found', from my small system.

<?php

class Core
{

    public function __construct()
    {

        $url = '/';
        if (!empty($_GET['url'])){
            $url .= $_GET['url'];
        }

        $params = [];

        if (!empty($url) && $url != '/'){
            $url = explode('/', $url);
            array_shift($url);

            $currentController = $url[0];
            array_shift($url);

            if (!empty($url[0])){
                $currentAction = $url[0];
                array_shift($url);
            } else {
                $currentAction = 'index';
            }

            if (count($url) > 0){
                $params = $url;
            }

        } else {
            $currentController = "Home";
            $currentAction     = "index";
        }

        // Not found
        if (!file_exists("../app/controllers/".$currentController.".php") ||
            !method_exists($currentController, $currentAction)){
                $currentController = "NotFound";
                $currentAction     = "index";
        }

        $currentController = new $currentController();

        call_user_func_array([$currentController, $currentAction], $params);

    }

}

This is the code of my Core, I learned to do it with Bonikey, and the structuring of folders I used, I learned from Brad, from the Traversy channel.

No answers

Browser other questions tagged

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