Laravel 4 removing public from URL
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/