How to remove public/index.php/ from url in Codeigniter 4 - php

I want normal URL like www.sample.com/controller not www.sample.com/public/index.php/controller.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php/$1 [L]
My .htaccess file

Just do
Step 1: In public/ directory, copy index.php and .htaccess to your root project directory.
Step 2: In the root project directory, open index.php and edit the following line:
index.php -> $pathsPath = FCPATH . '../app/Config/Paths.php';
to
index.php => $pathsPath = FCPATH . 'app/Config/Paths.php';

okay , this is how I solved this problem :
you need 2 .htaccess files
the first one in root path "/.htaccess" of your project
the second one in the public folder "public/.htaccess"
and here is the first one "/.htaccess"
# this is my version (Nassim) of the htaccess file , I use this one in the root directory "/"
# of the project and a second .htaccess in the public directory
DirectoryIndex /public/index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./public/index.php/$1 [L,QSA]
and the second one (comes with CI installation): "public/.htaccess"
# I recommend you remove `IfModule`. Because if you need mod_rewrite,
# you don't need `IfModule`. If you don't need it, you don't need this file
# at all.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|images|assets|doc|data|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

go to app/Config/App.php and then public $indexPage = 'index.php'; change to public $indexPage = '';
works like charm

Create a file called index.php outside of the public directory and write the command
require_once './public/index.php';
That's it.

Set up Virtual hosts (if you haven't already), but make sure to point to /public directory (xampp example):
ServerAdmin webmaster#example.com
ServerName example.com
DocumentRoot "path-to-project/public"
<Directory "path-to-project/public">
Order allow,deny
Allow from all
AllowOverride All
Require all granted
</Directory>
Do not touch any .htaccess file in default CI4 distribution
Edit app/Config/App.php - $baseURL = 'http://www.example.com/'; $indexPage = '';

I've no idea how you get this .htaccess. If you download CodeIgniter 4 from here and you can see Htaccess file is different what you added.
Since there are many changes in CodeIgniter (1,2,3) to Codeigniter 4, htaccess moved to public/ folder which will set to document root and .htaccess which place outside will not valid in CI 4
Correct path to .htaccess
public/.htaccess
Note: I just tested index.php URL issue and there was no issue like that. worked/loaded fine without index.php

Project Structure codeigniter4
If Run project in Xamp show Like this
Copy public-->index.php to rootfolder-->index.php
Now run Like this in webbrowser show this like error to solve this
solution
$pathsConfig = FCPATH . '../app/Config/Paths.php';
to
$pathsConfig = FCPATH . 'app/Config/Paths.php';
Now run
create .htaccess in root folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Now you can switch your controller name without .htaccess your url must be like this http://localhost:8080/payinlinks/index.php/login
else http://localhost:8080/payinlinks/login

Step 1:
Go into Your_Project_Folder > App > Config > App.php
change to $baseURL='http://localhost/Your_Project_Name'
change to $indexPage=""
Step 2:
Go into Your_Project_Folder > Public
Copy .httaccess and index.php
Paste these files in Your_Project_Folder
Step 3:
Go into Your_Project_Folder > index.php
change, FCPATH . '../app/Config/Paths.php' to FCPATH . 'app/Config/Paths.php'
ALL DONE!!!

create a .htaccess file in /public with :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Step 1: In public/ directory, copy index.php and .htaccess to your root project directory.
Step 2: In the root project directory, open index.php and edit the following line:
change:
$pathsPath = FCPATH . '../app/Config/Paths.php';
change TO
($pathsPath = FCPATH . 'app/Config/Paths.php';)
enter image description here

have you tried this:
RewriteEngine On
RewriteBase /yoursite.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php/$1 [L]

I was able to "solve" the problem in my hostinger server in one project. After I created a new project with a newer version of codeigniter 4 I realized that I could no longer use the same solution and after hours of research I found a very simple solution.
What I did is I changed the root directory to be the public folder using the .htaccess that is located at the root folder of the project.
#RewriteEngine on
#RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC,OR]
#RewriteCond %{HTTP_HOST} ^mydomain.com$
#RewriteCond %{REQUEST_URI} !public/
#RewriteRule (.*) /public/$1 [L]
Using that trick I could load my codeigniter 4 in a live server without any problem.
Actually, I used this to configure a codeigniter 4 project inside a subdomain.

change the index.php =>
require realpath(FCPATH . 'app/Config/Paths.php') ?: FCPATH . '../app/Config/Paths.php';
to
require realpath(FCPATH . 'app/Config/Paths.php') ?: FCPATH . 'app/Config/Paths.php';

Go to app/Config/App.php and find public $indexPage = 'index.php';.
Remove index.php from this public variable and that's it.

To solve the above issue removing public/index.php from url then you should following those below steps:
Copy the index.php and .htaccess files from the public/ directory.
Add the files to the root project directory.
Change your root project directory index.php file $pathsPath = realpath(FCPATH . '../app/Config/Paths.php'); into $pathsPath = realpath(FCPATH . 'app/Config/Paths.php');
Here is the full code of index.php file:
// Valid PHP Version?
$minPHPVersion = '7.2';
if (phpversion() < $minPHPVersion)
{
die("Your PHP version must be {$minPHPVersion} or higher to run CodeIgniter. Current version: " . phpversion());
}
unset($minPHPVersion);
// Path to the front controller (this file)
define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR);
// Location of the Paths config file.
// This is the line that might need to be changed, depending on your folder structure.
$pathsPath = realpath(FCPATH . 'app/Config/Paths.php');
// ^^^ Change this if you move your application folder
/*
*---------------------------------------------------------------
* BOOTSTRAP THE APPLICATION
*---------------------------------------------------------------
* This process sets up the path constants, loads and registers
* our autoloader, along with Composer's, loads our constants
* and fires up an environment-specific bootstrapping.
*/
// Ensure the current directory is pointing to the front controller's directory
chdir(__DIR__);
// Load our paths config file
require $pathsPath;
$paths = new Config\Paths();
// Location of the framework bootstrap file.
$app = require rtrim($paths->systemDirectory, '/ ') . '/bootstrap.php';
/*
*---------------------------------------------------------------
* LAUNCH THE APPLICATION
*---------------------------------------------------------------
* Now that everything is setup, it's time to actually fire
* up the engines and make this app do its thang.
*/
$app->run();

In CodeIgniter version 4.2 or above you have to follow 3 steps:
1st Step:
Go to this folder
App -> config -> app.php
Update base_url with your project folder name like we do in Codeigniter 3
Like this:
public $baseURL = 'http://localhost/your_project_name/';
In case if setup ci 4 using composer then you have this type of base_url which you need to change with the above defined base_url:
public $baseURL = 'http://localhost:8080';
2nd Step:
Go to public folder and copy index.php and .htaccess files (remember copy them not cut them form here)
Then paste them on root directory
Note: Same as we remove index.php in Codeigniter 3 by creating .htaccess file on the root folder and paste related code on `.htaccess' file.
3rd Step:
Replace root folders .htaccess file code with this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Then open root folders index.php file and check line 20 or this line:
require FCPATH . '../app/Config/Paths.php';
and remove ../ from it like this:
require FCPATH . 'app/Config/Paths.php';
In case you created css js folder in public folder and connected them using base_url on views/header.php views/footer.php or views/index.php then might be it occurs problems and don't get these files because of related problems:
When we use bootstrap cdn it gives this syntax which we made local like this but because of id="bootstrap-style" this it occurs problem so remove this form this line:
<link href="<?php echo base_url();?>/public/assets/css/bootstrap.min.css" id="bootstrap-style" />
Also form other css linked file remove this type="text/css" or any id="" it occures problem.
The right way to add css link is to define rel="stylesheet" on link like this:
<link rel="stylesheet" href="<?php echo base_url();?>/public/assets/css/bootstrap.min.css" />
In my case I got bootstrap and css related problems.

Related

How to solve Error 404 when deploying laravel project to Cpanel?

I'm trying to deploy my laravel (Laravel Framework 7.28.3) to Cpanel, but got a 404 error.
I uploaded my project into /public_html, modified the index.php file to point to the correct files (as below).
I think there must be some mistake in the index.php file but couldn't figure it out.
This is my first time asking my question (after searching for it several times), so hope that I will get the answer!
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
this is my file structure in File Manager:
Deploy laravel application in Cpanel
Setup 1 : - upload file to Cpanel the root directory – not the public_html.
Setup 2 : - Open the that folder and MOVE the CONTENTS of the public folder to your cpanel’s public_html .
Setup 3 : - Navigate to the public_html folder and locate the index.php file. Right click on it and select Code Editor from the menu.
and change this line
require __DIR__.'/../folderName/vendor/autoload.php';
$app = require_once __DIR__.'/../folderName/bootstrap/app.php';
NOTE : - folderName here is in root where you laravel application stay
that's it now all your request will come inside public_html folder index.php and this file will include require __DIR__.'/../folderName/vendor/autoload.php; and run laravel application
Folder structure will look like
/laravel
/public_html/index.php
indside index.php
require __DIR__.'/../laravel/vendor/autoload.php';;
$app = require_once __DIR__.'/../laravel/bootstrap/app.php'; // here laravel is folder name
You need to make sure your application is in a folder outside of your public_html.
Then you need to make a symbolic link to everything in your public directory inside your application. This symbolic link should be placed in your public_html.
This way your business logic is not available from the outside, only from your own application.
Actually it is advisable to clone your application using git and then install it following the steps in de docs. (https://laravel.com/docs/7.x/installation)
In your public/.htaccess file replaces the code given below..make sure that you uploaded your project in the root public_html folder.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

URL Routing in PHP : url routes ony to 404-Not-Found page

I have a php project in htdocs folder named "das-xampp". The index file in the root directory works as a router. I have another index.php inside views.
so the structure is as below:
das-xamp
|__index.php
|__views
|__index.php
|__about-us.php
|__404-Not_Found.php
So whenever someone types 'localhost/das-xampp' it should re-route the user to the index inside 'views/index.php'
My root index(the one that works as a router) is as follows:
<?php
$path = trim($_SERVER['REQUEST_URI'], '/');
parse_url($path, PHP_URL_PATH);
$routes = [
''=> 'views/index.php',
'about-us' => 'views/about-us.php'
];
if (array_key_exists($path,$routes)) {
require $routes[$path];
}else {
require 'views/404-Not-Found.php';
}
?>
The thing is whenever I type in 'localhost/das-xampp'(after turning on apache and mysql), the not-found php comes up. Even when I type 'localhost/das-xampp/about-us' manually Object not found is shown.
This doens't happen if I use
"php -S localhost:<some_digit>"
All my view works fine.
not-found page
The problem was with .htaccess file. I didn't include an .htaccess file which will re-route according to the routings I had in index.php file(the one in the root directory).
I'm posting the .htaccess code below
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /das/index.php [L]

Remove public_html from url through htaccess

I have a legacy project on a server where the actual framework is in a folder above the public_html folder. So the directory structure is like this:
domainname.com
app/
framework/
public_html/
index.php
Now I want to place this on our own server. This is an application created by the hosting company. The root folder is default. So now my directory structure is like this:
default/
app/
framework/
public_html/
index.php
Now he's pointing to the default folder instead of the public_html folder. That's the only difference with the old version. So I've added a .htaccess file to the root folder default. The content of this file is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
</IfModule>
In my public_html folder I have a .htaccess file like this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule . index.php [QSA,NS]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule . /index.php [L,QSA,NS]
So when I go to my website it works. But when I click on a link I get: http://domainname.com/public_html/theclickedpage/.
But I don't want the public_html in my url. How can I fix this?
You may want to search for a way to make a virtual host. I know some of my webhost let me configure the vhost through my admin page. It will let your server consider the url without including the specified folder, but back in the stage it performs needed redirection.
You applied correct rule ,
but the problem is link for the solution of this issue you have to
remove public_html from
your all links and it will automatically redirect to your required
path without public_html.

Remove public from URL Laravel 5.3

I'm using Laravel 5.3 is there a easy way to remove public from URL? is it necessary to add a htacces. when I change the htacces it is getting a n error like below,
copy both htaccess and index.php file from public folder to root directory and then change root index.php code as below
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
After installing Laravel, you should configure your web server's document / web root to be the public directory. The index.php in this directory serves as the front controller for all HTTP requests entering your application.
https://laravel.com/docs/5.3/installation#configuration
Do not edit .htaccess or Laravel related files, just point web server to a public directory instead of Laravel project directory:
DocumentRoot "/path_to_laravel_project/public"
<Directory "/path_to_laravel_project/public">
Then restart Apache.
Add a .htaccess file in root which contains -
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
Yes it is very simple no need to add extra htaccess,
Create a new folder in your laravel project folder.
Name it whatever you like. Example :- source
Move all the files from root into 'source' folder except 'public' folder.
4 . Move all the files from public folder to root.
5 . Change the autoload.php path
6 . Thats all. remove public folder if necessary.
In the root directory create a .htaccess file and put the following in
it
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Even above solution(s) work(s) for you,but I would recommend not to do this.
Here is the reason why.
-You should be pointing your Apache host root to the $LARAVEL_PATH/public directory instead of $LARAVEL_PATH.
-you have to configure apache for user not access .env, git files , config folder's file
public directory is there for reason to make project a bit more secure. Solve the actual problem and don't try to break this simple but nice security addition.
Copy all files and folder from public folder to root folder and edit your .htaccess file as
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
And dont forget to change the paths in index.php file.
For details refere following links
Installing Laravel in a subfolder
https://ellislab.com/forums/archive/viewthread/151342/#734511

Laravel .htaccess rewrite URL for removing public

I have a Laravel project
My domain is domain.com.tw
and the URL will be domain.com.tw/public,
and the page could be showed.
I want to remove the public in URL,
so I write RewriteRule in .htaccess with:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
but it did not work for me
I had look at these also, but still failed
Apache Mod Rewrite For Laravel
Laravel 4.2 rewrite URL - remove public
could someone tell me the solution? thanks!!
Create a folder on root with named “whatever”(you can give any name).
Move all files except public folder in the “whatever” folder.
Move all the files of public to root directory and remove blank public folder.
so directory structure will be:
whatever– which have all root files except public folder
all files of public folder.
Now time to change some paths
in paths.php change the following code
'public' => __DIR__.'/../public', into 'public' => __DIR__.'/../../',
open index.php (on root) and find below code
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
change this to
require __DIR__.'/local/bootstrap/autoload.php';
$app = require_once __DIR__.'/local/bootstrap/start.php';
Hope this helps or at least give you some idea

Categories