Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 637
Kevin Jolan Avatar asked Mar 23 '13 11:03

Kevin Jolan


People also ask

How can I remove public Index PHP in the URL generated Laravel?

Create or edit the . htaccess file in the Laravel root directory and configure the RewriteRule to remove “public/index. php” from the URL. You must have mod_rewrite enable on your Apache server.

What is URL Helper in Laravel?

Laravel provides several helpers to assist you in generating URLs for your application. These helpers are primarily helpful when building links in your templates and API responses, or when generating redirect responses to another part of your application.


2 Answers

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

like image 59
Nirmal Garia Avatar answered Oct 20 '22 00:10

Nirmal Garia


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
like image 35
HomeSlice Avatar answered Oct 19 '22 23:10

HomeSlice