Home > Mobile >  How to remove public? from url in php project
How to remove public? from url in php project

Time:07-14

I am currently working to build a small php mvc framework. in a framework i have a this folder structure.

-app
  --controllers
    -Post.php
-core
-logs
-public
  --.htaccess
  -- index.php
-vendor

in here index.php is working as Front Controller

in post controller is look like this..

<?php

/**
 * Posts controller
 *
 */
class Posts
{

    public function index()
    {
        echo 'Hello index';
    }

    public function addNew()
    {
        echo 'Hello addNew';
    }
}

in url, i want to remove project/public/?posts/index public/?. When i remove (public/?) and visit the url. its showing me this error message.

project/posts/index

The requested URL was not found on this server.

using public/? project/public/?posts/index is working fine. and its echo index message

project/public/

The .htaccess inside of the public folder contains:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

in project main root folder ... i did't added .htaccess and index.php file.

in .htaccess when i add this line. url redirect to xammp welcome screen

RewriteRule ^(.*)$ index.php?$1 [L,QSA]

CodePudding user response:

I'd say you want to internally rewrite all incoming requests to the controller inside the /project/public folder. But that is not what you do. The rule you implemented (RewriteRule ^(.*)$ index.php?$1 [L,QSA]) only rewrites relative to the requested folder. No mentioning of "public" in there.

The actual setup you need depends a bit on your http host setup here. Where its DOCUMENT_ROOT points to. Most likely to the folder that contains the file system structure you posted in your question. If so you should implement a rule that rewrites all incoming requests to the /project/public folder.

Something like that, though you probably need to tweak it to match your actual setup:

RewriteEngine on
RewriteRule ^ /public/index.php?%{REQUEST_URI} [L,QSA]

You can implement such rule in the http server's host configuration. Or, if you do not have access to that, you can use a distributed configuration file (if you have enabled those for the http host), so a ".htaccess" style file. That file should be located inside the folder your http hosts DOCUMENT_ROOT setting points to. So the folder containing the file system structure your posted.

Other setups are possible, this is just one option. The point is: you need to rewrite the requests to your controller. Where the controller actually is.

  • Related