Laravel Application Development Cookbook
上QQ阅读APP看书,第一时间看更新

Creating "clean" URLs

When installing Laravel, the default URL we will use is http://{your-server}/public. If we decide to remove /public, we can use Apache's mod_rewrite to change the URL.

Getting ready

For this recipe, we just need a fresh installation of Laravel and everything running on a properly configured Apache server.

How to do it...

To complete this recipe, follow these steps:

  1. In our app's root directory, add a .htaccess file and use this code:
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteRule ^(.*)$ public/$1 [L]
    </IfModule>
  2. Go to http://{your-server} and view your application.

How it works...

This simple bit of code will take anything we add in the URL and direct it to the public directory. That way, we don't need to manually type in /public.

There's more...

If we decide to move this application to a production environment, this is not the best way to accomplish the task. In that case, we would just move our files outside the web root and make /public our root directory.