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]
Related
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.
I have below Url in my sample application in PHP MVC CI
http://localhost:1234/Sample/Index.php/User/index
Here User is Controller and index is Action Method
Can I write this url like below ?
http://localhost:1234/Users
After seeing your code , main mistake you have done you have renamed index.php, which is wrong, please revert that index1212.php to index.php
Rest of the thing you can follow other answers.But this is maon, codeigniter will not run without index.php file
Edit
One more error i noticed, in routes.php,
its
$route['Users'] = 'user/';
not $routes
And access your project like this,http://localhost/Sample/Users
And as far as my knowledge, you cannot hide folder name in url.
If you would like to use custom routes. http://www.codeigniter.com/user_guide/general/routing.html
You do not need to include index.php in routes.php
// application > controllers > sample > Users.php filename first letter must be uppercase. Same with class name.
$routes['users'] = 'sample/users/index'; // Lower case preferred.
// When with out function would go straight to index function
$routes['users'] = 'sample/users';
// No Need for index.php
// $routes['Users'] = 'Sample/Index.php/User/index';
Now url will be
http://localhost:1234/project/users
http://localhost/project/users
Or with index.php
http://localhost:1234/project/index.php/users
http://localhost/project/index.php/users
You may need to remove index.php here are some htaccess
https://github.com/riwakawebsitedesigns/htaccess_for_codeigniter
application > config > config.php
$config['index_page'] = '';
htaccess example
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Note WAMP users:
If your using WAMP make sure you have rewrite module enabled.
Sample is your project dir so you can not write the route which you want you can write the route as bellow.
http://localhost:1234/Sample/Users
And your route file will look like bellow.
$route['Users'] = 'User/index';
I am using Slim 2.4 and php 5.3. My Directory Structure in localhost is something like this
:
Web Root
AppName
API
v1
include
Slim <- Slim Framework
tools
.htaccess (#1)
index.php (#1)
Assets
css
js
img
.htaccess (#2)
index.php (#2)
I wan to access localhost/AppName/v1 that will open up localhost/AppName/API/v1. This is working partially as further parameters are getting ignored. When I am trying to get localhost/AppName/v1/anyOtherVariable it opens up localhost/AppName/API/v1 only.
Now, #1 index.php contains all Slim code (routing and stuff) but #1 .htaccess is blank.
#2 index.php contains a simple static homepage that contains some links. In #2 .htaccess I have written
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^v1(/.*)?$ API/v1/index.php$1 [QSA,L]
</IfModule>
I am really out of options.
A quick and dirty solution is to define a version constant and then remove it from the global variable $_SERVER['REQUEST_URI'].
For example my api url is http://localhost/api/v1/hello/firstname/lastname.
I defined a constant at then removed it from $_SERVER['REQUEST_URI'] as given below:
define('API_VERSION', '/api/v1');
$_SERVER['REQUEST_URI'] = str_replace(API_VERSION, '', $_SERVER['REQUEST_URI']);
$application->get (
'/hello/:firstname/:lastname',
....
And it worked like a charm :)
After head-banging 100 times, I decided to change my directory structure and right now, everything is working great. :) I am posting this answer to help others who alos have faced the same problem. So here's, my Directory Structure.
WebRoot /
AppName /
v0.2 /
v0.5 /
v0.9 /
_APP /
cache /
data /
libs /
vendor /
Slim /
userCake /
MeekroDB /
Acc / ( Seperate Routes )
API /
.htaccess
index.php
Assets /
images /
css /
js /
fonts /
Test /
.htaccess
404.php
config.php
index.php
Now, these are the codes I use inside the main files:
index.php under API contains Slim Routes.
.htaccess under APi contains the following code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA]
</IfModule>
index.php under v0.9 directory contains the homepage
.htaccess under he same directory contains:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
RewriteRule ^(_APP/) - [F,L,NC]
</IfModule>
This .htaccess restricts direct access to _APP folder as it contains sensitive data. Now people will tell me to move it outside the publicly accessible directory but for users with shared hosting like me, the hosting company restricts going outside the public root. Although using this setup secures the App But you should always try to move this _APP directory outside public directory.
and the main config.php contains:
<?php
// PROJECT SETTINGS
// Show Erros on Localhost and Local Ethernet only
$test_server = $_SERVER['SERVER_NAME'] == "127.0.0.1" || $_SERVER['SERVER_NAME'] == "localhost" || substr($_SERVER['SERVER_NAME'],0,3) == "192";
ini_set('display_errors',$test_server);
error_reporting(E_ALL|E_STRICT);
$_APP=array();
// META CONFIGURATIONS
$_APP= array_merge($_APP, array(
'meta'=>array(
'name'=>'expoWWW',
'ver'=>'v 0.0.2',
'url'=>'http://localhost/AppName/v0.9/',
'root'=>$_SERVER['DOCUMENT_ROOT'].'/',
'appUrl'=>'AppName/v0.9/',
'author'=>'Abhishek Deb (iam#debabhishek.com)',
'admin'=>''
)
));
// DATABASE CONFIGURATION
$_APP= array_merge($_APP, array(
'db'=>array(
'h'=>'localhost',
'u'=>'root',
'p'=>'root',
'd'=>'APpName'
)
));
// FUNCTIONS
// HOUSE-KEEPING
?>
Usage: I simply Include this config file in every php file and then I can use $_APP['db']['h']
I have a directory site and its sub-folder and files in it.
It also contain a .htacess and below is the code
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?cc=$1
so my below url will is effective and working good.
http://localhost/site/sweetinc
And below code in index.php below
<?php
if (isset($_GET['cc']) and !empty($_GET['cc'])) {
echo $_GET['cc'];
} else {
die("Sorry wrong code");
}
?>
And this is working good.
Now, i want to access or display all files as normally
http://localhost/site/sweetinc/home.php
http://localhost/site/sweetinc/test.php
home.php and test.php are located in site directory. And if it redirects to other sub-folders then it should
be visible like
http://localhost/site/sweetinc/sub-folder1/test3.php
Is this possible, as I am working seperating a group using seperate directory using .htaccess and considering all files in site directory as base files
The trick here (if I understand your question correctly) is that you don't want the rewrite rule to be in effect when you're trying to reach a file that actually exists, right?
In that case, just before your RewriteRule, add these lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
That way, the rewriterule only takes effect when the url (/site/sweetinc for example) does not really exist on the server. If it does (/site/sweetinc/home.php) the rewriterule is skipped and the file is shown.
(-f checks if the filename exists, -d checks if the directory exists, so /home/sweetinc/somedir/ should work too)
Update based on updated question
You need two separate rules for this. First of all, if the /sweetinc/ directory in the url is used, refer them to the /site/ folder:
RewriteRule ^sweetinc/(.*)$ /$1 [L]
Then, if the file does not actually exist, let the index.php handle it:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?cc=$1 [L]
Some examples:
I have the following files in my files subdomain:
/index.php (which has the same content as yours does, so it reads out ?cc=
/circle3.PNG
/the .htaccess with the above rules
http://files.litso.com/sweetinc/hello shows the index.php that echoes "hello"
http://files.litso.com/sweetinc/circle3.PNG shows the actual file in /
(note, I don't have a /sweetinc/ directory, it's all faked)
I installed Zend framework 1.11.5 I have Wamp 2.2 and running windows7.
I have a folder (mysite) inside apache documentRoot('c:/wamp/www');'mysite' folder structure is:
/application
/controllers
IndexController.php
/views
/scripts
index.phtml
bootstrap.php
/library
/public
/css
/images
/javascript
.htaccess
index.php
Issue: If I point the browser to 'http://localhost/mysite/public/' I can see my index page correctly.But if I point to 'http://localhost/mysite/public/index/' or 'http://localhost/ejoin2ED/public/index/index' I see the Wampserver configuration page (I tought this is the output of another page I have, 'index.php' inside 'c:/wamp/www' ).
Shouldnt I see the content of index.phtml instead??
thanks
Luca
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
index.php
//identify the location of th application dir in respect to
//the botstrap file's location, and configure PHP's include_path to
//include the library directory's location
define('APPLICATION_PATH',realpath(dirname(__FILE__).'/../application/'));
set_include_path(APPLICATION_PATH.'/../library'.PATH_SEPARATOR.
get_include_path());
//give the zend framework the ability to load classes on demand,
//as you request them,rather than having to deal with require() statements.
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
//retrieve the BOOTSTRAP file
try
{
require'../application/bootstrap.php';
}
catch(Exception $exception)
{
printf('Could not locate bootstrap.php');
exit(1);
}
//start using the front controller in order to route all requests
Zend_Controller_Front::getInstance()->dispatch();
bootstrap.php
//configure the site environment status
defined('APPLICATION_ENVIRONMENT')
or define('APPLICATION_ENVIRONMENT','development');
//invoke the front controller
$frontController=Zend_Controller_Front::getInstance();
//identify the location of the controller directory
$frontController->setControllerDirectory(APPLICATION_PATH.'/controllers');
//create the env parameter so you can later access the environment
//status within the application
$frontController->setParam('env',APPLICATION_ENVIRONMENT);
//clean up all allocated script resources
unset($frontController);
There is a problem with your htaccess file.
The slash at index.php is the problem
RewriteRule ^.*$ /index.php [NC,L]
Should be
RewriteRule ^.*$ index.php [NC,L]