do not find mvc class

Advertisement

This is my project .htaccess in mvc

 RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] 

This is my simple class in the controllers folder

 class index { function __construct() { echo "this page is index"; } } 

and this is my index page and I want in the controller part in url to be index, it prints the controller part and runs the class. But these are mistakes:

 Undefined index: url 

and

 require(controllers/.php): failed to open stream: No such file or directory 

My code:

 $url = $_GET['url']; $url = rtrim($url,'/'); $url = explode('/',$url);//breaks string to array require 'controllers/'.$url[0].'.php'; $controller = new index(); print_r($url); 

The answer

Do not forget to rewrite the "base" for redirection ( RewriteBase ).
Delete the line RewriteCond %{REQUEST_FILENAME} !-l . Does a -l
Does the option even exist?

Here is an example with a document root "Publics", where the file "index.php" is located:

 Options FollowSymLinks # Uncomment if you want to only allow localhost. # Allow from 127.0.0.1 # Activate rewriting engine. RewriteEngine On # Allow pin-pointing to "index.php" using RewriteRule. RewriteBase / # Rewrite url only if no physical folder name is given in url. RewriteCond %{REQUEST_FILENAME} !-d # Rewrite url only if no physical file name is given in url. RewriteCond %{REQUEST_FILENAME} !-f # Take the common url and parse it through "index.php" as a query string, eg as "url=[...]". # ---------------------------------------------------------------------------------- # SEE: RewriteRule Flags >> https://httpd.apache.org/docs/current/rewrite/flags.html # ---------------------------------------------------------------------------------- RewriteRule ^(.+)$ index.php?url=$1 [QSA,L,B]  Options FollowSymLinks # Uncomment if you want to only allow localhost. # Allow from 127.0.0.1 # Activate rewriting engine. RewriteEngine On # Allow pin-pointing to "index.php" using RewriteRule. RewriteBase / # Rewrite url only if no physical folder name is given in url. RewriteCond %{REQUEST_FILENAME} !-d # Rewrite url only if no physical file name is given in url. RewriteCond %{REQUEST_FILENAME} !-f # Take the common url and parse it through "index.php" as a query string, eg as "url=[...]". # ---------------------------------------------------------------------------------- # SEE: RewriteRule Flags >> https://httpd.apache.org/docs/current/rewrite/flags.html # ---------------------------------------------------------------------------------- RewriteRule ^(.+)$ index.php?url=$1 [QSA,L,B] Options FollowSymLinks # Uncomment if you want to only allow localhost. # Allow from 127.0.0.1 # Activate rewriting engine. RewriteEngine On # Allow pin-pointing to "index.php" using RewriteRule. RewriteBase / # Rewrite url only if no physical folder name is given in url. RewriteCond %{REQUEST_FILENAME} !-d # Rewrite url only if no physical file name is given in url. RewriteCond %{REQUEST_FILENAME} !-f # Take the common url and parse it through "index.php" as a query string, eg as "url=[...]". # ---------------------------------------------------------------------------------- # SEE: RewriteRule Flags >> https://httpd.apache.org/docs/current/rewrite/flags.html # ---------------------------------------------------------------------------------- RewriteRule ^(.+)$ index.php?url=$1 [QSA,L,B] Options FollowSymLinks # Uncomment if you want to only allow localhost. # Allow from 127.0.0.1 # Activate rewriting engine. RewriteEngine On # Allow pin-pointing to "index.php" using RewriteRule. RewriteBase / # Rewrite url only if no physical folder name is given in url. RewriteCond %{REQUEST_FILENAME} !-d # Rewrite url only if no physical file name is given in url. RewriteCond %{REQUEST_FILENAME} !-f # Take the common url and parse it through "index.php" as a query string, eg as "url=[...]". # ---------------------------------------------------------------------------------- # SEE: RewriteRule Flags >> https://httpd.apache.org/docs/current/rewrite/flags.html # ---------------------------------------------------------------------------------- RewriteRule ^(.+)$ index.php?url=$1 [QSA,L,B]