Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

php - Apache Mod Rewrite For Laravel

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

C:wampwwwlaravel

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:wampinapacheYOUR APACHE VERSIONconfextra 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:windowssystem32driversetchosts" 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...