Separate CodeIgniter public folder - php

I've been trying to change my CodeIgniter file structure to make it safer and cleaner but I can't get it to work. I want to separate the application/system and the public files that are going to be visible to users.
- application
- system
- public
It works but I have to enter
example.com/public/index.php or example.com/public/controller
I want to be able to just type the base URL like example.com/controller.
How can I do it?

For CodeIgniter 3.x, the correct way to approach this (at this point in 2018 and after) is as follows:
Make a public folder in your root folder
MOVE your index.php file into this folder (to prevent ambiguity)
inside the moved index.php file, change the following:
change $system_path to ../system
change $application_folder to ../application
Now, we need an .htaccess file, but CI 3.x doesn't have a .htaccess file by default, soo.. how about stealing it from CI 4.x which has it by default? You can find it here:
https://github.com/bcit-ci/CodeIgniter4/blob/develop/public/.htaccess
I recommend you NOT include the line SetEnv CI_ENVIRONMENT development - instead, define this in your apache or NGinx .conf file so the .htaccess file can work anywhere.
Finally, you'll meed to update your .conf file so that DOCUMENT_ROOT is set to the subfolder named public (or whatever you chose to name it). Then restart Apache/Nginx.
That should give you the same results, and be much more secure.
-- UPDATE --
I found that I had problems with the .htaccess file from CI 4, however suspect it's my system that's the problem. This simple version did work however:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]

As per the CodeIgniter Installation Instructions...
/var/application/
/var/system/
/var/www/index.php
For the best security, both the system and any application folders should be placed above web root so that they are not directly accessible via a browser. By default, .htaccess files are included in each folder to help prevent direct access, but it is best to remove them from public access entirely in case the web server configuration changes or doesn’t abide by the .htaccess.

Related

Laravel redirect issue for custom root folder

My ISP doesn't allow my cpanel hosted laravel site to have a custom root folder (the public folder within Laravel's structure).
This means I can't make the server see /public_html/public as the root. It has to always be /public_html
Following their guidance I have setup the following /public_html/.htaccess rule to make the Laravel public folder appear as the root folder
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
This works more or less but NOT completely. The issue is that if I request a page on my site with an ending backslash, the page url ends up as
www.domain-name.com/public/contact-us
instead of what I would expect which is
www.domain-name.com/contact-us
I assume this must be a problem with the .htaccess but I've tried various permutations and can't resolve it. I maybe wrong though, maybe it's something that ultimately needs to be fixed by a Laravel redirect after a url check.
It's driving me mad, I'd really appreciate any ideas!
Putting the entire application inside a public_html folder is a big red flag for security purposes. You have to be very very cautions with your permissions inside this folder. Things like .env, .git, cache files, private uploads, etc are usually downloadable by default and yes hackers will be scanning for treats.
If you can upload your entire app to the root of your cpanel host and rename public to public_html (or copy the contents of public into the existing public_html if you cannot remove it)
Then you may have to modify your laravel app index.php to use this as it's public path.
In index.php under :
$app = require_once __DIR__.'/../bootstrap/app.php';
add:
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
With this the standard .htaccess should work without changes.
Hope that helps.

Codeigniter run external PHP Script in a separated Folder

I have a problem with Codeigniters Security.
My Codeigniter Installation has the Application Folder, System Folder and a Assets Folder.
In my Assets Folder there is a Third Party PHP Script.
I want to Call this Script: DOMAIN/assets/FOLDEROFEXTERNALSCRIPT/EXTERNALPHPSCRIPT.php
Is there a option that i can call this File over the URL without a Controller?
I hope you have removed index.php from your url's which is done by either adding the below rewrite rules in the .htaccess file at your DOMAIN root directory, or by adding these rewrite rules in the virtual hosts.
Below rule means, to rewrite every url to index.php?params except if the current url contains "index.php or assets in it", now you can put any static content or even core PHP scripts in this folder to be access directly, with having CI in picture.
In your .htaccess file just add "assets" folder in the bypass rule, along with index.php
RewriteEngine on
RewriteCond $1 ^!(index\.php|assets)
RewriteRule ^(.*)$ /index.php?$1 [L,QSA]

Laravel 4 removing public from URL

So, I'm running xampp on Windows. I'm currently trying to get familiar with the laravel framework. Now, when thats pointed out. How can i be able to access my laravel application/website direct within the root?
Example,
What I'm doing now is: localhost/laravel/public/about (to see the
about page)
What i want to do is: localhost/laravel/about
Any good solutions for this? do i need to add a .htacess file on the root folder of laravel? (not the public one).
Any suggestions?
Easiest way is create .htaccess file in your Laravel root with following content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
It should be redirected easily.
Reference: https://coderwall.com/p/erbaig/laravel-s-htaccess-to-remove-public-from-url
Here's how I did it.
Edit your Windows Host file - C:\Windows\System32\drivers\etc\hosts
Edit the Apache vhosts file - Drive-Letter:\xampp\apache\conf\extra\httpd-vhosts.conf
Add an htaccess file to the laravel/public folder (if its not already there)
Restart Xampp apache server
Windows can be a real PITA when trying to edit the Hosts file because of the User Account Control. Since I work on all kinds of small hobby projects, I have to edit this file all the time so this is what I do.
Install PSPad. It loads really fast and you can bookmark files for easy loading/editing. Sublime Text also works well if you load the two files I mentioned above and save the workspace as a new project.
Right-click on the PSPad (or other editor) program shortcut and choose 'Run as Administrator'. You cannot save changes to the Hosts file unless you do this.
Open the Windows Host file in the editor. This file does not have a file extension, so you have to choose "All Files" in the File Open dialog to even see the file.
At the bottom of the file, add this:
127.0.0.1 laravel.dev
This tells Windows to point the web browser to localhost whenever you enter laravel.dev in the browser's address bar.
Save the file.
Open the xampp Apache httpd-vhosts.conf file.
At the bottom of the file, add this: (I am assuming xampp is installed at the root of the D: drive)
<VirtualHost *:80>
ServerName laravel.dev
DocumentRoot "D:/xampp/htdocs/laravel/public"
<Directory "D:/xampp/htdocs/laravel/public">
</Directory>
</VirtualHost>
Add an htaccess file to your laravel/public folder (if its not already there).
I think the default htaccess file that comes with L4 looks like this:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Restart your xampp apache server.
Open a web browser and type in the address bar - http://laravel.dev
That will take you to the index.php file in the "public" folder.
To get to the About page, I think the address would be http://laravel.dev/about
Move the contents of the /public folder down a level.
You'll need to update the include lines in index.php to point to the correct location. (if it's down a level, remove the '../').
BEST Approch: I will not recommend removing public, instead on local computer create a virtual host point to public directory and on remote hosting change public to public_html and point your domain to this directory. Reason, your whole laravel code will be secure because its one level down to your public directory :)
METHOD 1:
I just rename server.php to index.php and it works
METHOD 2:
Here is my Directory Structure,
/laravel/
... app
... bootstrap
... public
... etc
Follow these easy steps
move all files from public directory to root /laravel/
now, no need of public directory, so optionally you can remove it now
now open index.php and make following replacements
require DIR.'/../bootstrap/autoload.php';
to
require DIR.'/bootstrap/autoload.php';
and
$app = require_once DIR.'/../bootstrap/start.php';
to
$app = require_once DIR.'/bootstrap/start.php';
now open bootstrap/paths.php and change public directory path:
'public' => DIR.'/../public',
to
'public' => DIR.'/..',
and that's it, now try http:// localhost/laravel/
Set you document root for apache to the public folder, and not the laravel folder. This is the simplest technique and recommended for production environments.
I'm using L5, This works for me fine:
Rename the server.php in the your Laravel root folder to index.php
copy the .htaccess file from /public directory to your Laravel root folder.
-- Thatz it!!!
I've been struggling with this problem too but i've found a simple solution that only requires you to create another .htaccess at the root of your application.
The .htaccess itself should contain this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^{yoursite}.{extension} [NC]
RewriteRule (.*) http://www.{yoursite}.{extension}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
The complete system keeps working but it redirects to the /public folder.
This is how I solved the problem for myself.
Hope it helps!
Cheers.
Add following code to htaccess file. It may helps you.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Add following code in your .htaccess (if not exist create a .htaccess on laravel root directory)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Source : http://tutsnare.com/remove-public-from-url-laravel/
at Source you also get another method to do same.
Update : Preferred way to do it is make change in directory structure which explain in source URL.
just in simple step i did in laravel 5
make .htaccess like this in laravel folder
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ ./index.php [L]
</IfModule>
then rename your server.php to index.php
that it it will work
or if you just doing local development
run this comman php artisan serve
it will start local server at localhost:8000 (port may vary)
You can use symlinks or edit the httpd.conf file.
Check my answer to another similar question. I hope that it helps.
If you don't wish to go through the stress of configuring .htaccess file,
you could use PHP Built-in Server by doing this:
From your command utility, cd into laravel\public
The run: php -S localhost:8000
After you can access your website by going to:
http:://localhost:8000
works without appending public
See the official manual to learn more:
http://php.net/manual/en/features.commandline.webserver.php
Go to project folder using cmd and type "php artisan serve".
Now navigate to: localhost:8000
I have found geart flow to work with laravel localy.
What you can do is to configure xampp a bit. At your xamp's httpd.conf file you have to find document DocumentRoot and <Directory>. Change root directory to yours laravel public folder and restart apache. Since when you can access your project simplly just typing localhost. Now if you want you can change your host file and rewrite local dns, for example: 127.0.0.1 example.laravel.com and now you can access your project with real url. It may look bit complicated, but it's not.
Alternative to that would be php artisan serve. You can start server on different ports and when re-write hosts file.
You could add some features to improve your workflow even more, for example vagrant or ngrok. You can share your project for live presentation (speed may be issue here).
Need to remove public segment in the larvel4 app
Laravel 4 requires you to put your app code one level higher than the web root, and this causes problems for some developers that are stuck on shared hosting and that doesn’t allow a setup like this. It’s actually really easy to get around it. I read that some L4 specific packages could have problems on a setup like this, but I didn’t experience anything like that with any package yet.
So first install L4 somewhere you like. I liked the article Niall wrote on keeping the base L4 app up to date, so go and check that out: Installing and Updating Laravel 4
I find it’s enough for this example to simply clone the repo (assuming you have composer installed globally, if not, go to http://getcomposer.org/):
git clone -b develop git://github.com/laravel/laravel.git app_name
php composer install
Note that we are cloning the develop branch since L4 is still in beta at this time.
So to remove the “public” part from your URL, simply move all files and folders from public to your app root and you’ll end up with a folder structure like this:
/app
/bootstrap
/packages (copied from /public)
/vendor
.htaccess (copied from /public)
artisan
composer.json
favicon.ico (copied from /public)
index.php (copied from /public)
robots.txt (copied from /public)
server.php
Now we need to edit our paths in index.php:
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/start.php';
And then just set the public dir in out /bootstrap/paths.php file:
'public' => __DIR__.'/..',
this is my suggession
You need to do following things:
first copy all the contents of the public directory in your root directory i.e. bring the contents of public folder 1 step up.
modify the contents of index.php
From =>
require __DIR__ . "/../bootstrap/autoload";
$app = require_once __DIR__ . "/../boostrap/start.php"
To =>
"require __DIR__.'/bootstrap/autoload.php';"
"$app = require_once __DIR__.'/bootstrap/start.php';
and also contents of bootstrap/paths.php
From => 'public' => __DIR__.'/../../',
To => 'public' => __DIR__.'/..',
3.Then finally create .htaccess file in your root directory and write this.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Simple Steps To follow:
Rename server.php (In Root directory) to index.php
Copy .htaccess file from public directory to root directory
rename the server.php to index.php and copy .htaccess from /public is the right way.
If you send your app online,just change DocumentRoot to the path of public.
if you remove public from url first of all move index.php and .htaccess file from public folder to root of the laravel and change in index.php file
require DIR.'/../bootstrap/autoload.php';
$app = require_once DIR.'/../bootstrap/start.php';
to
require DIR.'/bootstrap/autoload.php';
$app = require_once DIR.'/bootstrap/start.php';
and run the program
This has been asked before many times. I had the same problem. I solved it by using vhosts and .htaccess files. I wanted to write about solution on both XAMPP on Windows and LAMP installation on Ubuntu. My configuration on windows:
My aim was to reach my application by entering the uri below
http://localhost/subdir
c:\xampp\htdocs\subdir\ # this was laravel root directory
c:\xampp\apache\conf\extra\httpd-vhosts.conf # this virtual hosts file
I used original .htaccess file from Laravel website (and remember .htaccess file must be in public directory) but I just added one line which is
RewriteBase /subdir (just below RewriteEngine On)
In addition, in httpd-vhosts file you should add your subdirectory alias like that:
Alias /subdir
"C:/xampp/htdocs/subdir/public"
<Directory "C:/xampp/htdocs/subdir/public">
Order allow,deny
Allow from all
Options All
AllowOverride All
Require all granted
</Directory>
Hope everything is clear with my explanation. This answer can be applied on unix based systems easily.

How to access folder via browser if codeigniter project exists?

I have codeigniter project lets say example.com. In my public_html folder on my server, i created a folder called test and i want to be able to access some file via the web using example.com/test/somefile.php
Since i also have a codeigniter project in the same public_html directory, i always get a codeigniter page not found error when i try example.com/test/somefile.php.
How can i basically tell my server or codeigniter project that the folder "test" is not part of the codeigniter app?
In your .htaccess at the root of public_html, you probably have something like this for routing everything through index.php:
Example taken from the CI user guide: http://codeigniter.com/user_guide/general/urls.html
RewriteEngine on
# If the path doesn't start with one of these...
RewriteCond $1 !^(index\.php|images|robots\.txt)
# ...send the request to index.php/REQUEST
RewriteRule ^(.*)$ /index.php/$1 [L]
Just add "test" to the pipe delimited group, or whatever you need to do to allow access to the directory with your configuration:
RewriteCond $1 !^(index\.php|test|images|robots\.txt)
# ^^^^
Without this CI requires index.php in the URL - there's no way to actually have your Codeigniter app itself redirect, rewrite URLs, or block access to the /test directory, it's generally all done through an .htaccess file.

.htaccess - mod_rewrite is working for some pages but not for others

I am stuck into a weird problem.
I have a file at the location /public_html/academics/courses.php
I want .htaccess to mod_rewrite the URLs as below:
Original URL: http://niecdelhi.ac.in/academics/courses/
After mod_rewrite: http://niecdelhi.ac.in/index.php?inc=/academics/courses/
What I want, basically, is to mod_rewrite all URLs to index.php and pass the URL as a parameter named "inc". Then, in the index.php I include the file by doing include($_GET['inc']);
mod_rewrite is working for some pages on the website. and I am getting the URL in $inc. But, it is not working at all for other pages.
For example, consider the two files that exist on the server:
http://niecdelhi.ac.in/academics/courses.php
http://niecdelhi.ac.in/academics/library.php
mod_rewrite is working for the first, the file gets included in index.php
But for the second I get the plain existing file. not the one included in index.php
I hope you understand the problem that I am facing. Please provide me with the solution.
.htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !\..*$ [NC]
RewriteCond %{QUERY_STRING} (.*) [NC]
RewriteRule ^(.*)$ index.php?inc=$1&%1 [L]
Page working: http://niecdelhi.ac.in/academics/courses/
Page not working: http://niecdelhi.ac.in/academics/library/
*EDIT*
There is no other .htaccess anywhere. Although, I have found a clue about what is happening. The problem is happening only in Linux server. The code is working correctly in Windows server.I have a Linux server with PHP 5.2.16.
Also, regarding some pages working and some not. I have found that only those pages are working which have a folder with identical name in the same directory. For example, The academics directory is as below:
academics/
|_ courses/
| |_ mba.php
| |_ mca.php
|_ courses.php
|_ library.php
Now, Since courses.php has a folder with identical name in same directory. It gets mod_rewrite fine. But library.php is not getting mod_rewrite.
Linux server is skipping the mod_rewrite for the files that actually exist. Why so ??
My only guess is that there is another .htaccess somewhere in your structure (most obviously inside /academics/ ) that is overriding the rule for "library".
Could there be another mod_rewriting rule inside that folder that is kicking in for the word LIBRARY and probably messing up your rewriting.
Note that it might also be a native apache issue. For example, in ubuntu, by default in version 10.10 (i think thats it) if you had a /javascript/ folder, it would be short circuited to /usr/lib/javascript or something like that...
Check all possible instances of mod rewrite in httpd.conf, all dynamicaly loaded .conf files, your vhost file and finaly the path of your document root...

Categories