Ody
Ody

Reputation: 2047

Apache Mod Rewrite For Laravel

I have an installation of Laravel on Wampserver. The directory is as follows:

C:\wamp\www\laravel

Now URLs are like this:

http://localhost/laravel/public/index.php/home/index

So I used the following htaccess code

Options +FollowSymLinks
Options -indexes
DirectoryIndex index.PHP
RewriteEngine on
RewriteCond $1 !^(index\.PHP|images|robots.txt)
RewriteCond %{REQUEST_ FILENAME} !-f
RewriteCond %{REQUEST_ FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L, QSA]

To reduce the URL to

http://localhost/laravel/public/home/index

But the laravel framework insists that all application files reside in the public folder.

So I would like to know what I need to add to (or subtract from) the htaccess file so that the URL can look like

http://localhost/laravel/home/index

Thanks

Upvotes: 13

Views: 48369

Answers (5)

Relaxing In Cyprus
Relaxing In Cyprus

Reputation: 2006

The easiest way I got this working on my local dev environment was to do the following:

(Assuming you have WAMP installed in C:\WAMP)

Create the following folder:

c:\wamp\www\laravel

Download laravel and put the contents in the above directory. You will know you have done it right if you can browse to hxxp://localhost/laravel/public and get the opening screen. However, this isn't good enough. We want to get that screen by going to http://localhost/laravel

So then we do the following:

Create a textfile containing the following:

Alias /laravel "c:/wamp/www/laravel/public" 

<Directory "c:/wamp/www/laravel/public">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
    Allow from all
</Directory>

Save this file as laravel.conf in the c:\wamp\alias directory.

Finally, restart your wampserver.

You should now be able to surf to http://localhost/laravel

Note that the above is strictly for a local development environment.

Upvotes: 1

Jason Lewis
Jason Lewis

Reputation: 18665

When testing locally I do one of two things.

  1. Create a new .htaccess below the public directory with the following.

    <IfModule mod_rewrite.c>
        RewriteEngine on
    
        RewriteRule ^(.*)$ public/$1 [L]
    </IfModule>
    
  2. Create a new virtual host. With WAMP you can navigate to C:\wamp\bin\apache\YOUR APACHE VERSION\conf\extra and find your httpd-vhosts.conf file, in there you can see example virtual hosts. Here's one of mine:

    <VirtualHost *:80>
        DocumentRoot "c:/wamp/www/laravel/public"
        ServerName laravel.dev
        ServerAlias www.laravel.dev
    </VirtualHost>
    

    Make sure that your vhosts configuration file is being included. Open up your httpd.conf file and search for the vhosts file, uncomment the include line if it's commented out. Then I open the CLI and enter notepad "C:\windows\system32\drivers\etc\hosts" which opens up your hosts file. Underneath the item that mentions localhost place your new host. Here's an example.

    127.0.0.1  laravel.dev
    

    Make sure you restart Apache and bingo, you should be able to navigate to http://laravel.dev and you won't have any annoying public directory. This is how I achieve it, as I prefer the nicer looking virtual host rather then a long winded localhost URL.

Hope this helps.

Upvotes: 25

Ody
Ody

Reputation: 2047

I finally figured a way out. First of all, I had to open and edit my Apache httpd.conf by selecting it from the Wamp Aestran tray menu. The I had to uncomment the line

#Include conf/extra/httpd-vhosts.conf

After that, I opened the file which is located at the

<wampdirectory>/bin/apache/apache.x.y.z/conf/extra/httpd-vhosts.conf

then I added the following lines.

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80


<VirtualHost *:80>
    DocumentRoot "C:/wamp/www"
    ServerName localhost
    Options Indexes FollowSymLinks
    <Directory "C:/wamp/www">
        AllowOverride All
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
        #If you want to allow access from your internal network
        # For specific ip addresses add one line per ip address
        #Allow from 192.168.0.100
        # For every ip in the subnet, just use the first 3 numbers of the subnet
        #Allow from 192.168.0
    </Directory>
</VirtualHost>

## must be first so the the wamp menu page loads when you use just localhost as the domain name

<VirtualHost *:80>
    DocumentRoot "C:/wamp/sites/laravel/public"
    ServerName laravel.dev
    Options Indexes FollowSymLinks
    <Directory "C:/wamp/sites/laravel/public">
        AllowOverride All
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
        #If you want to allow access from your internal network
        # For specific ip addresses add one line per ip address
        #Allow from 192.168.0.100
        # For every ip in the subnet, just use the first 3 numbers of the subnet
        #Allow from 192.168.0
    </Directory>
</VirtualHost>

The next step was to edit my hosts file at C:\windows\system32\drivers\etc

and added

127.0.0.1 laravel.dev

Then restarted Wamp and it worked. Thanks to you guys for pointing me in the right direction. Really Appreciate it

Upvotes: 4

Ralph Frost
Ralph Frost

Reputation: 81

As a newb to WAMP and Laravel, I struggled at bit but did get the virtualhost thing to work on my WIN7PRO 64-bit box. In WAMPSERVER/Apache/hppd.conf at the end of the file, I added:

NameVirtualHost *:80

<VirtualHost *:80>
 DocumentRoot C:/webapp/public
 ServerName webapp
 <Directory C:/webapp/public >
   Options Indexes FollowSymLinks MultiViews
   AllowOverride all
   Order Deny,Allow
   Allow from All
 </Directory> 
</VirtualHost> 

<VirtualHost *:80>
 DocumentRoot C:/wamp/www
 ServerName localhost
</VirtualHost> 

and I added:

127.0.0.1 webapp

to the hosts file. (I was never successful editing the vhosts files, as many posts on the web suggested.)

These changes allow me to get to my Laravel test app in my browser via

http://webapp
(and also, via just http://127.0.0.1)

and, to get to all my other sites, via:

http://localhost/devsite/whatever..

Upvotes: 0

J&#252;rgen Paul
J&#252;rgen Paul

Reputation: 14997

You're gonna end up with your code and your public folder residing in the same place, which most people do not recommend. I'd suggest you take advantage of using a local web server.

Why not make mysite.dev point to laravel/public directory so you could just use http://mysite.dev everytime, you have cleaner and shorter URL's too?

Upvotes: 0

Related Questions