Makeing Laravel work in Root

Okay, I wasn’t sure what to call this tutorial, I could of called it Laravel Pretty URLs or Getting Rid Of Public, but I didn’t, so Making Laravel Work in Root will have to do and for SEO purposes, I already mentioned the other titles, ha ha. The gist of this tutorial is that it will teach you how to change ugly stock Laravel URLs that look like:

TO look like this:

The first example URL is what my first public Laravel installation produced. I had to navigate to the project name, laravel, then to the public folder and finally to the route I named in routes.php. Not very nice at all if you ask me or my client, in this case, So, how do II make the king of advertising happy? Yes! Pretty URLs of course! Here is how it’s done with Laravel:

Method 1:

you will have to edit your virtual host declaration in your server’s config file. This is often in either httpd.conf or a file included in the httpd.conf file such as vhosts.conf or something similar. If you are not sure, search the include files and directories from your httpd.conf file which is normally in etc/httpd/conf/ directory or as in my case, I had a client with a public server that didn’t allow access so I had to use a different solution. But if you are lucky enough to have a VPS or other server with httpd.conf access, simply edit the document root to that of your new public directory.

Method 2:

This may not be the preferred method, but it works if you would like to have a directory structure like:

where everything is inside the app directory or whaever you want to call it. I am not referring to thte existing app directory in laravel, but a new one you can create to hold app, vendor, bootstrap, etc. So, what you do is upload all your Laravel files to a folder named app or whatever you want to call it that is inside of your web root directory so you have something like to hold Laravel files. Then you move all the files from public down one level to yourfolder in this example. Then you have to edit the index.php file paths. Open index.php and replace any occurences of ../ with just / and you are done.


Leave a Reply

Your email address will not be published. Required fields are marked *