Laravel .htaccess rewrite URL for removing public - php

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

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

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

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.

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 called from ROOT outsite the /public folder. Getting NotFoundHttpException

I've searched through Web to solve my problem but non of the solutions works for me there, so after couple of hours of struggling with Routing module I've decided to ask you for a helping hand.
Problem
I am unable to access Laravel application from outside /public directory. I need to type localhost\projects\laravel\public in my browser but what I want is to use the URL withour /public. The main reason is because I use shared hosting and have no access to apache configuration file so I'm unable to create vhost.
Background
I've installed manually Laravel framework under the: **c:\xampp\htdocs\projects\laravel** and used composer to do the rest for me.
The routing has been set to: Route::get('/', 'HomeController#showWelcome');
In my project root directory **c:\xampp\htdocs\projects\laravel** I've put a .htacces file with the following content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /projects/laravel/
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
In my Application Root dir c:\xampp\htdocs\projects\laravel\public I have .htaccess with the following code in it:
<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>
When I type: localhost\projects\laravel\ I've got NotFoundHttpException
When I type: localhost\projects\laravel\public I've got welcome page
Any help would be appreciate, Thanks.
Finally I've got this thing working. There are two solutions that can be made.
One requires to move all -public content level-up
Second requires slightly changes in your shared hosting directory root structure - if allowed
Solution #1
The first that works for me that I didn't want to implement has been posted by #Wasim in this thread: Laravel 4 removing public from URL The solution is not save as the content core structure is in the same directory as application itself. This could cause some problems in future implementation.
You need to move all the content from public/ folder one level-up into project ROOT directory then replace internal paths in index.php file for correct onece. For security reasons this .htaccess file needs to be put into project ROOT directory
<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>
Modified Laravel structure described above can be uploaded to your shared web hosting and the application should runs withour /public in yor http address.
Solution 2 (with no access to http.conf file on your shared hosting)
This solution does not requires form you to move the content of the /public folder one level-up but requires form you to have read and write access to ../ROOT directory in your shared hosting (../ROOT directory mostly contains public_html, public_ftp and other folders).
You need to move Laravel scructure into ../ROOT directory as follows:
app/
bootstrap/
vendors/
public_html/
public_ftp/
(...)
Files form /public folder goes to public_html
public_html/index.php
public_hmtl/.htaccess
public_html/packages/
(...)
Then modification for /bootstrap/paths.php is required for line with 'public' key:
'public' => DIR.'/../public' to 'public' => DIR.'/../public_html'
If someone has similar issue and this solution does not work please let me know, thanks.
If you want without public on uri, using composer from your laravel root. Use command php artisan serve and browse localhost:8000 from your browser. It will bring you to homepage.
Where did you get this line from?
RewriteRule ^(.*)$ public/$1 [L]
It's not part of Laravel. Try removing it or the whole .htaccess file in your Laravel root.

Avoid public folder of laravel and open directly the root in web server

I was going through all possible sample on internet to solve this. Still it is an headache.
I just want to avoid the 'public' in www.mylaravelsite.com/public/
and make it like www.mylaravelsite.com for the root directory.
Now I do not want to avoid the security concern,So I learned .htaccess would be the best way.
Any solution friends ?
& advance thanks for interacting !
Let's assume you have this folder structure in your server
.cpanel/
public_html/
public_ftp/
..
And the laravel folder structure is
app/
bootstrap/
public/
vendor/
composer.json
artisan
..
You can create a folder name mylaravelsite on your server inline with public_html and public_ftp folder, and copy to it the whole laravel application except the public folder because you will paste all of it contents on the public_html, so you have now:
.cpanel/
public_html/
public_html/packages
public_html/vendor
public_html/index.php
public_html/.htaccess
...
public_ftp/
mylaravelsite/
mylaravelsite/app
mylaravelsite/bootstrap
...
On your public_html/index.php change the following line:
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
to
require __DIR__.'/../mylaravelsite/bootstrap/autoload.php';
$app = require_once __DIR__.'/../mylaravelsite/bootstrap/start.php';
and also don't forget to change /mylaravelsite/bootstrap/paths.php public path, you might use it.
'public' => __DIR__.'/../public',
to
'public' => __DIR__.'/../../public_html',
Your site should be running.
Create .htaccess in root folder and write these line
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
For production server it is recommended that you point your domain directly to the public folder
It sounds like the information you are missing is:
Documentroot
This is also called the "web root". Apache specifically calls it the DocumentRoot - Click that link for the official explanation.
Basically what it does is set which directory the server pulls files from. In Laravel, the document root should be the public folder.
Setting the DocumentRoot to public means that going to http://mylaravelsite.com in the browser will infact be "pointing" to the public folder as you want.
In the .htaccess file (actually, more likely in the virtual host configuration), you can set the DocumentRoot for your site:
DocumentRoot /path/to/laravel-app/public
How you set this up depends on your hosting. Each hosting has different ways to setup a website and so we cannot answer that specifically for you - you should consult your web hostings tech support. The key point is that the public directory should be the web root, while everything else should be "behind the web root".
Fair warning tho: some hosting providers do not give you enough access to accomplish that.
Just add .htaccess file on Laravel root if it's not present and add following lines in it, that's it,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
create .htacess file in the root directory of your laravel project and put this code in.
<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
point your web directory to public/. If you are using apache this will work:
<VirtualHost *:80>
DocumentRoot /var/www.mylaravelsite.com/public/
ServerName www.mylaravelsite.com
</VirtualHost>
Here is the solution, But Must not do it in real projects, it is just for starter to test Laravel and i have tested it with laravel 5.0 and it is ,cut index.php and .htaccess files from public folder and paste it in the root directory and change these two lines as
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
It is Highly Recommended not to use the above method without testing environment, and should use virtual host instead.
All you just need to change .env file in your laravel project files. if your laravel project inside in
Root/domain/public_html/laravel/
Go to .env file and edit
you need to edit app url path which should be
APP_URL=http://www.domainname.com/laravel/public/
Thats it your project will run now. if your laravel files is in public_html, then your app url must be like this
APP_URL=http://www.domainname.com
Thanks
For Lumen
Let's think you have a folder path like: /var/www/project/microservice(lumen src)
/var/www => document root for localhost/
Target you want:
localhost/project/microservice[/foo...] => localhost/project/microservice/public/foo...
I do this by .htaccess (placed at /project folder) like below:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*/public/.*
RewriteCond %{REQUEST_URI} ^(/[^/]+/[^/]+/).*
RewriteRule ^[^/]+(.*)$ %1public$1 [L,R=301,P]
On Server(Apache)
Create .htaccess in the root folder and write below line
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
Lean more on Gist
On Local Development
You can create a virtual host by adding the below line to your apache vhost config file.
Generally, you can find this file in C:\xampp\apache\conf\extra\httpd-vhosts.conf file
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/mysite/public/"
ServerName www.mysite.local
</VirtualHost>

Categories