codeigniter index.php removal - codeigniter

I have a multi city application and have the following setup for my application
This is outside the server root
framework
backend
frontend
CI_2.0.1
system
Now in server root i have 2 folders to represent 2 cities
html
city1
city1.php
city2
city2.php
I need to add a rule to get rid of the .php in the url so it looks more like
http://example.in/city1/search
http://example.in/city2/search
Or a rule to completely get rid of the index.php file

To index.php and some more .htaccess trick I use this code (CI 2):
# Deny OR Allow Folder Indexes.
# Since we disable access to PHP files you
# can leave this on without worries.
# OR better yet, create a .htaccess file in
# the dir you want to allow browsing and
# set it to +Indexes
Options -Indexes
Options +FollowSymLinks
# Set the default file for indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
# mod_rewrite rules
RewriteEngine on
# If a controler can't be found - then issue a 404 error from PHP
# Error messages (via the "error" plugin)
# ErrorDocument 403 /index.php/403/
# ErrorDocument 404 /index.php/404/
# ErrorDocument 500 /index.php/500/
# Deny any people (or bots) from the following sites: (to stop spam comments)
# RewriteCond %{HTTP_REFERER} nienschanz\.ru [NC,OR]
# RewriteCond %{HTTP_REFERER} porn\.com
# RewriteRule .* - [F]
# Note: if you are having trouble from a certain URL just
# add it above to forbide all visitors from that site.
# You can also uncomment this if you know the IP:
# Deny from 192.168.1.1
# If the file is NOT the index.php file
RewriteCond %{REQUEST_FILENAME} !index.php
# Hide all PHP files so none can be accessed by HTTP
RewriteRule (.*)\.php$ index.php/$1
# If the file/dir is NOT real go to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
</IfModule>
# If Mod_ewrite is NOT installed go to index.php
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
P.S. I think that I didn't get quite clearly your problem with .php files. But if you haven't already, you'd look at native CI routing system it may solve your problem with another approach)

Let’s start with our big URL:
http://mycustomproject.com/index.php/webpages/view/my-url-title-of-the-post
The first thing that you usually do for your Codeigniter project is to remove the index.php file from the URL. To do it so, it’s really easy:
1st step
Add this code to your .htaccess file to your root of your project.
<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On
# If your website begins from a folder e.g localhost/my_project then
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
RewriteBase /
# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(application|system|private|logs)
# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|css|js|images)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
After the creation of the .htaccess file your project structure will have to look like this:
website_folder/
–––– application/
–––– assets/
–––– system/
–––– user_guide/
---- .htaccess <-------------------------
–––– index.php
–––– license.txt
2nd step
Now go to : application/config/config.php and change the:
$config['index_page'] = 'index.php';
to
$config['index_page'] = '';
So now your URL will look like this:
http://mycustomproject.com/webpages/view/my-url-title-of-the-post

Related

Laravel: All pages but index are NOT FOUND

I have just deployed my app to an AMI ec2 server and following some tutorials, got to one that explains to modify config/app.php like:
/*
|--------------------------------------------------------------------------
| Application URL
|--------------------------------------------------------------------------
|
| This URL is used by the console to properly generate URLs when using
| the Artisan command line tool. You should set this to the root of
| your application so that it is used when running Artisan tasks.
|
*/
'url' => env('APP_URL', 'http://52.67.251.19/'),
... and change httpd.conf as follows:
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html/nnid/public/"
#
# Relax access to content within /var/www.
#
<Directory "/var/www/nnid/public/">
AllowOverride None
# Allow open access:
Require all granted
</Directory>
But all pages, besides the landing page, are NOT FOUND. Any hints?
Make sure you have mod_rewrite turned on on your server.
Also Laravel 5.6 ships with this .htaccess file inside of the public folder.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Just make sure it matches your current one.
SOLVED!
The problem was at the httpd.conf at mode ReWrite. Changed to All and works like a breeze!

Laravel 5.5. .htaccess file [duplicate]

I know this is a very popular question but I haven't been able to find a working solution for Laravel 5. I've been trying to migrate from Codeigniter for a long time, but this convoluted installation process keeps putting me off.
I don't want to run a VM, this just seems awkward when switching between projects.
I don't want to set my document root to the public folder, this is also awkward when switching between projects.
I've tried the .htaccess mod_rewrite method
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
This just gives me a Laravel NotFoundHttpException in compiled.php line 7610.
When I tried L4 a while ago, I used the method of moving the contents of the public folder into the root. The structure of L5 is quite different and following the same steps completely broke Laravel (the server would only return a blank page).
Is there a decent method of removing 'public' in a development environment that:
Works with L5
Allows me to switch between projects with ease (I'm usually working on 2 or 3 at any one time).
Thanks
** I'm using MAMP and PHP 5.6.2
For Laravel 5:
Rename server.php in your Laravel root folder to index.php
Copy the .htaccess file from /public directory to your Laravel root
folder.
That's it!
PLEASE NOTE when serving a Laravel project with Docker: you won't need to do any of this. Only use this option when your root (or more commonly: public_html) directory of your website is your Laravel project (this is not the case when you're using Docker).
DON'T!
YOU REALLY SHOULD NOT rename server.php in your Laravel root folder to index.php
and copy the .htaccess file from the /public directory to your Laravel root folder!!!
This way everyone can access some of your files (.env for example). Try it yourself. You don't want that!
DO
Instead, you should create an .htaccess file in your root like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
This will silently rewrite all your base URIs to the /public folder. Even all Headers, for example the HTTP Authorization Header, and all optional URI parameters will silently be passed on to the /public folder as well.
That's all
I have solved the issue using 2 answers:
Renaming the server.php to index.php (no modifications)
Copy the .htaccess from public folder to root folder
(like rimon.ekjon said below)
Changing .htaccess it a bit as follows for statics:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
If there are any other static files needed just add the extension to the previous declared list
In Laravel 5.5 create .htacess file in your root directory and placed the following code:- Reference Link
<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
</IfModule>
Create .htaccess file in root directory and place code something like below.
<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
</IfModule>
Easy way to remove public from laravel 5 url.
You just need to cut index.php and .htaccess from public directory and paste it in the root directory,thats all
and replace two lines in index.php as
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
Note: The above method is just for the beginners as they might be facing problem in setting up virtual host and The best solution is to setup virtual host on local machine and point it to the public directory of the project.
#rimon.ekjon said:
Rename the server.php in the your Laravel root folder to index.php and copy the .htaccess file from /public directory to your Laravel root folder. -- Thats it !! :)
That's working for me.
But all resource files in /public directory couldn't find and request urls didn't work, because I used asset() helper.
I changed /Illuminate/Foundation/helpers.php/asset() function as follows:
function asset($path, $secure = null)
{
return app('url')->asset("public/".$path, $secure);
}
Now everything works :)
Thank you #rimon.ekjon and all of you.
2020 Author's Update
This answer is not recommended.
Instead, handling .htaccess file is recommended.
Just create .htaccess file at root and add these lines to it
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
That's it!
The above code works on the root public_html folder. Having said that your core laravel file should be inside the public_html folder, if your directory looks like public_html/laravelapp/public and if you put the above code inside laravelapp it won't work. Therefore you must copy all your core files into public_html and put the .htaccess file there .
If you want to keep the code in a subdirectory then you can create a subdomain then this code will work for that also.
1) I haven't found a working method for moving the public directory in L5. While you can modify some things in the bootstrap index.php, it appears several helper functions are based on the assumption of that public directory being there. In all honestly you really shouldn't be moving the public directory.
2) If your using MAMP then you should be creating new vhosts for each project, each serving that projects public directory. Once created you access each project by your defined server name like this :
http://project1.dev
http://project2.dev
It's possible to remove public url in laravel5. Follow these steps:
step 1.copy all file from public and paste on root directory
step 2.open index.php file
replace with
require __DIR__.'/../bootstrap/autoload.php';
to
require __DIR__.'/bootstrap/autoload.php';
and
$app = require_once __DIR__.'/../bootstrap/app.php';
to
$app = require_once __DIR__.'/bootstrap/app.php';
And remove all cache and cookies.
4 best ways to remove public from the URL.
If you used any other trick to remove the public from the URL like changes the name of server.php to index.php and changing into the core file path. Clearly, don't do that. Then why Laravel not giving the solution like this because it's not a proper way to do that.
1) Remove public from URL using htaccess in Laravel
By adding a .htaccess file into the root, You can access the website without public
<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
</ifmodule>
2) Remove the public by creating a virtual host in your local
I am giving demo here for the Window operating system. But I will try to define a step so that anyone can easily follow the step. You can also research on google for the same for the particular operating system.
Step 1: Go to C:\Windows\system32\drivers\etc\ open the "hosts" file in Administrator mode.
Step 2: Add the following code to it. Here, I am giving you a demo of projectname.local domain name demo, you can specify any as you like. Just make it constant at every place.
127.0.0.1 projectname.local
Step 3: Now go to, C:\xampp\apache\conf\extra for xampp users and for the wamp user "C:\wamp\bin\apache\Apache2.4.4\conf\extra" and open "httpd-vhosts.conf" file. Now add the following code into it.
Notes: Change the Document root as per your project also add domain name as you define into the "hosts" file.
<VirtualHost projectname.local>
ServerAdmin projectname.local
DocumentRoot "C:/xampp/htdocs/projectdir"
ServerName projectname.local
ErrorLog "logs/projectname.local.log"
CustomLog "logs/projectname.local.log" common
</VirtualHost>
Step 4: Last but the important step is to restart your Xampp or Wamp and access the url like http://projectname.local and your Laravel will respond without public URL.
3) Remove the public by running the command in Laravel
If you are working in local then you don't need to do anything just need to run the following command from your terminal or command line tool. After that, you can access your website by provided URL by the command line.
> php artisan serve
If you are willing to run your project on particular IP then you need to run following command. If you are working on LAN then if you want to allow other people to access your website from local then you just need to check your IP address using command line by running "ipconfig" after getting your IP address run following the command.
> php artisan serve --host=192.168.0.177
If you are willing to run your project on a particular IP with particular port then you need to the following command.
> php artisan serve --host=192.168.0.177 --port=77
4) Remove the public on the hosted server or on the cpanel
After completion of the project you need to host the project on the server, then you just need to set the document root on your domain to the public folder. Check the below screenshot.
As per screenshot if you don't have any project folder into the public_html then you just need to set your document root like "public_html/public".
Reference taken from here
Here is the Best and shortest solution that works for me as of may, 2018
for Laravel 5.5
just cut your .htaccess file from the /public directory to the root directory and replace it content with the following code:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
</IfModule>
Just save the .htaccess file and that is all.
Rename your server.php file to index.php. that is all enjoy!
Let's say you placed all the other files and directories in a folder named 'locale'.
Just go to index.php and find these two lines:
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
and change them to this:
require __DIR__.'/locale/bootstrap/autoload.php';
$app = require_once __DIR__.'/locale/bootstrap/app.php';
BEST Approach:
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:
This work well for all laravel version...
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/
I would like to add to #Humble Learner and note that the proper location to "fix" the url path for assets is /Illuminate/Routing/UrlGenerator.php/asset().
Update the method to match:
public function asset($path, $secure = null)
{
if ($this->isValidUrl($path)) return $path;
$root = $this->getRootUrl($this->getScheme($secure));
return $this->removeIndex($root).'/public/'.trim($path, '/');
}
This will fix scripts, styles and image paths. Anything for asset paths.
For XAMPP user to remove public from url without touching laravel default filesystem is to set a Virtual Host for your application to do this jsut follow these steps
Open the XAMPP control panel application and stop Apache. Be aware that late Windows machines might run it as a service, so check the box to the left of the Apache module.
Navigate to C:/xampp/apache/conf/extra or wherever your XAMPP files are located.
Open the file named httpd-vhosts.conf with a text editor.
Around line 19 find # NameVirtualHost *:80 and uncomment or remove the hash.
At the very bottom of the file paste the following code:
<VirtualHost *>
ServerAdmin admin#localhost.com
DocumentRoot "C:/xampp/htdocs" # change this line with your htdocs folder
ServerName localhost
ServerAlias localhost
<Directory "C:/xampp/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Now you can copy and paste the code above below to add your Virtual Host directories. For example I’m working on a site called Eatery Engine so the following snippet will allow me to work with sub-domains on my local install:
<VirtualHost eateryengine.dev>
ServerAdmin admin#localhost.com
DocumentRoot "C:/xampp/htdocs/eateryengine" # change this line with your htdocs folder
ServerName eateryengine.dev
ServerAlias eateryengine.dev
<Directory "C:/xampp/htdocs/eateryengine">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Next head over to your Windows host file to edit your HOSTS. the file will be located at C:/Windows/System32/drivers/etc/hosts, where hosts is the file. Open it with notepad.
Look for #localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
127.0.0.1 eateryengine.dev #change to match your Virtual Host.
127.0.0.1 demo.eateryengine.dev #manually add new sub-domains.
Restart Apache and test everything.
The original article can be found here
There is always a reason to have a public folder in the Laravel setup, all public related stuffs should be present inside the public folder,
Don't Point your ip address/domain to the Laravel's root folder but point it to the public folder. It is unsafe pointing the server Ip to the root folder., because unless you write restrictions in .htaccess, one can easily access the other files.,
Just write rewrite condition in the .htaccess file and install rewrite module and enable the rewrite module, the problem which adds public in the route will get solved.
Create new file called .htaccess in root project folder and place the below code inside it :
<IfModule mod_rewrite.c>
#Session timeout
<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
</IfModule>
Note: If you changed server.php to index.php you also should rename it in above code
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Activate the mod_rewrite module with
sudo a2enmod rewrite
and restart the apache
sudo service apache2 restart
To use mod_rewrite from within .htaccess files (which is a very common use case), edit the default VirtualHost with
sudo nano /etc/apache2/sites-available/000-default.conf
Below "DocumentRoot /var/www/html" add the following lines:
<Directory "/var/www/html">
AllowOverride All
</Directory>
Restart the server again:
sudo service apache2 restart
Problem is if you type /public and it will still be available in the url, therefor i created a fix which should be placed in the public/index.php
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
if(stristr($uri, '/public/') == TRUE) {
if(file_exists(__DIR__.'/public'.$uri)){
}else{
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$actual_link = str_replace('public/', '',$actual_link);
header("HTTP/1.0 404 Not Found");
header("Location: ".$actual_link."");
exit();
return false;
}}
this peace of code will remove public from the url and will give a 404 and then redirects to the url without the public
I know that are are a lot of solution for this issue. The best and the easiest solution is to add .htaccess file in the root directory.
I tried the answers that we provided for this issue however I faced some issues with auth:api guard in Laravel.
Here's the updated solution:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
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
</IfModule>
Create the .htaccess file in your root directory and add this code. And everything goes right
Create .htaccess file in root directory and place code something like below.
<IfModule mod_rewrite.c>
#Session timeout
<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
</IfModule>
Create .htaccess file in /public directory and place code something like below.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Create A .htaccess file in your root DIR and paste the below code. That's it :P
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
Even if i do not recommend putting Laravel on the root folder there are some cases when it could not be avoided;
for those cases the above methods do not work for assets so i made a quick fix changing the htaccess:
after copying server.php to index.php edit the .htaccess file like so:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
### fix file rewrites on root path ###
#select file url
RewriteCond %{REQUEST_URI} ^(.*)$
#if file exists in /public/<filename>
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
#redirect to /public/<filename>
RewriteRule ^(.*)$ public/$1 [L]
###############
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
#RewriteCond %{REQUEST_FILENAME} -d # comment this rules or the user will read non-public file and folders!
#RewriteCond %{REQUEST_FILENAME} -f #
RewriteRule ^ index.php [L]
</IfModule>
This was a quick fix i had to make so anyone is welcome to upgrade it.
Laravel 5.5
After having installed Laravel for the first time, I faced the renowned "public folder problem" and I came up with this solution that, in my personal opinion, is "cleaner" then the others I found on the Web.
Achievements
Don't have public word in the URI
Protect the .env file against curious people
Everything can be done just editing the .htaccess using mod_rewrite and four simple rules.
Steps
Move the .htaccess file in public/.htaccess in the main root
Edit it as below
I commented everything, so it should be clear (I hope) also to those who have never used mod_rewrite (not that I'm an expert, all the opposite). Also, to understand the rules, it must be clear that, in Laravel, if Bill connects to https://example.com, https://example.com/index.php is loaded. This file just contains the command header("refresh: 5; https://example.com/public/"), which sends the request to https://example.com/public/index.php. This second index.php is responsible to load the controller and other stuff.
# IfModule prevents the server error if the app is moved in an environment which doesn’t support mod_rewrite
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# RULES ORIGINALLY IN public/.htaccess ---
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# --- END
# PERSONAL RULES ---
# All the requests on port 80 are redirected on HTTPS
RewriteCond %{SERVER_PORT} ^80$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
# When .env file is requested, server redirects to 404
RewriteRule ^\.env$ - [R=404,L,NC]
# If the REQUEST_URI is empty (means: http://example.com), it loads /public/index.php
# N.B.: REQUEST_URI is *never* actually empty, it contains a slash that must be set as match as below
# .* means: anything can go here at least 0 times (= accepts any sequence of characters, including an empty string)
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*) /public/index.php [L]
# If the current request is asking for a REQUEST_FILENAME that:
# a) !== existent directory
# b) !== existent file
# => if URI !== css||js||images/whatever => server loads /public/index.php, which is responsible to load the app and the related controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(css|js|images|media)/(.*)$ /public/index.php [L,NC]
# If the current request is asking for a REQUEST_FILENAME that:
# a) !== existent directory
# b) !== existent file
# => if URI == css||js||images[=$1]/whatever[=$2] => server loads the resource at public/$1/$2
# If R flag is added, the server not only loads the resource at public/$1/$2 but redirects to it
# e.g.: bamboo.jpg resides in example.com/public/media/bamboo.jpg
# Client asks for example.com/media/bamboo.jpg
# Without R flag: the URI remains example.com/media/bamboo.jpg and loads the image
# With R flag: the server redirects the client to example.com/public/media/bamboo.jpg and loads the image
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(css|js|images|media)/(.*)$ /public/$1/$2 [L,NC]
# --- END
</IfModule>
The following rule (originally in public/.htaccess) can be deleted. The same rule, in fact, is explicited in a more detailed way in the last two rules.
# Handle Front Controller...
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule ^ index.php [L]
EDIT: I missed Abhinav Saraswat's solution and his answer should be the accepted one. Just one, simple and clear rule that redirects all the traffic to the public folder without modifying any file.
Firstly you can use this steps
For Laravel 5:
1. Rename server.php in your Laravel root folder to index.php
2. Copy the .htaccess file from /public directory to your Laravel root folder.
source: https://stackoverflow.com/a/28735930
after you follow these steps then you need to change all css and script path, but this will be tiring.
Solution Proposal :simply you can make minor change the helpers::asset function.
For this:
open vendor\laravel\framework\src\Illuminate\Foundation\helpers.php
goto line 130
write "public/".$path instead of $path,
function asset($path, $secure = null){
return app('url')->asset("public/".$path, $secure);
}
I have read some article before and it's working fine but really don't know is safe or not
a. Create new folder local.
b. Move all project into the local folder expect public folder.
c. Move all the content of public folder to project root.
d. Delete the blank public folder
f. Edit the index file.
Edit the index.php
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
to
require __DIR__.'/local/bootstrap/autoload.php';
$app = require_once __DIR__.'/local/bootstrap/app.php';
Another method that I use is to create a symbolic link (in Linux, don't know about Win) using the ln command in htdocs or www i.e. ln projectname/public project
The site is thus accessible via localhost/project
You can remove public keyword from url using various methods.
1) If you are using dedicated hosting and you have root access then You can remove public keyword from url using Virtual Host. You should give DocumentRoot path with public. So this will start index from public directory and remove it from url.
<VirtualHost *:80>
ServerAdmin info#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html/{yoursourcedirectory}/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
2) If you dont have root access of your hosting then you should genarate a new .htaccess file in your root directory and put the code as below
<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
</IfModule>
You can get more reference here.
I found the most working solution to this problem.
Just edit your .htaccess in the root folder and write the following code. Nothing else required
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
<IfModule php7_module>
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 1000
php_value memory_limit -1
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php71"
php_value upload_max_filesize 2M
php_flag zlib.output_compression Off
</IfModule>

Handling codeigniter application on server

I have CODEIGNITER site live and running successfully on a server, but when I move(copy and paste) the contents in another folder on the same server I am getting 404 page not found error.
Any ideas why?
Use this htaccess in the project folder not in application folder
<IfModule mod_rewrite.c>
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/test1
# then use
# RewriteBase /mypage/test1/
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
If your folder is not follow like that public_html/project_folder
than change here
RewriteBase project_folder/

Laravel sets wrong public path with a index.php file is requested in a subdirectory

In Laravel 4.2 using Apache 2.4, if I access an index.php different from index.php in the site root then the public path is set wrong. For instance, the html document returned by /site/index.php is right, but all its routes to assets are relative to /site. For instance,
File system path: /var/www/htdocs/imgs/logo.png
Right url: /imgs/logo.png
Real path: /site/imgs/logo.png
This issue was discovered because the old site had you index file in /site/index.php and was indexed by the search engines. Then when the site is shown in the search results, it shows a link to site/index.php with all the assets paths broken.
The paths to assets are set with Laravel's function as HTML::style or assets
How do I redirect all the index.php files inside a subpath to index.php in the root file, except this last?
The default .htaccess of Laravel 4.2 is
<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 I add a new Rewrite Rule before #Handle Front Controller
# Redirect to index.php if other index.php distict to the index.php is requested.
RewriteCond %{REQUEST_URI} ((.+)/)+index.php [NC]
RewriteRule ^ / [R=301]
So, I redirect (permanently) to the site root to any index.php in a subdirectory.

Apache not properly routed for Laravel

I have moved my index.php for Laravel from /public to /public/api. Right now, if I want to access it, I have to type the index.php in the URL as well.
For instance, to access a resource, I have to type localhost/api/index.php/resource otherwise I get a 404 error.
Can someone please help? The .htaccess works fine on my friend's computer who is running a MAC, but it doesn't work on my Windows 8 computer:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /api/
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I highly recommend that you do not do this as I can see no reason that you should be altering where the index.php file is placed. You will likely run into other issues when making your application as well. With that said you need to modify the index.php file to contain the following lines. Notice the extra ../ since you have moved it up one directory.
require __DIR__ . '/../../bootstrap/autoload.php';
$app = require_once __DIR__ . '/../../bootstrap/start.php';
Also make sure the .htacess file is placed inside the same folder as your index.php file.
I should also note you should not have to modify the .htacess file at all for this. Remove the
RewriteBase /api/
line from your .htacess.

Resources