Laravel 4 removing public from URL

32,861

Solution 1

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

Solution 2

Here's how I did it.

  1. Edit your Windows Host file - C:\Windows\System32\drivers\etc\hosts
  2. Edit the Apache vhosts file - Drive-Letter:\xampp\apache\conf\extra\httpd-vhosts.conf
  3. Add an htaccess file to the laravel/public folder (if its not already there)
  4. 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

Solution 3

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 '../').

Solution 4

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

  1. move all files from public directory to root /laravel/
  2. now, no need of public directory, so optionally you can remove it now
  3. 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';

  1. 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/

Solution 5

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.

Share:
32,861
Kevin Jolan
Author by

Kevin Jolan

Updated on July 31, 2022

Comments

  • Kevin Jolan
    Kevin Jolan almost 2 years

    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?