12

Im using laravel 4.

I have a view nest.blade.php and the corresponding controller NestController.php:

Controller content:

class NestController extends BaseController {

    public function showView()
    {
        return View::make('nest');
    }

}

Route:

Route::get('/nest', 'NestController@showView');

When I go to url/nest it doesnt work. When I go to url/index.php/nest it does work.

Obviously I just want it to be /nest without the index.php.

How can i resolve this?

My htaccess:

IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
tereško
  • 56,151
  • 24
  • 92
  • 147
RSM
  • 12,215
  • 34
  • 89
  • 138
  • Based on your .htaccess it seems you are running Laravel 4.1? Anyhow, I copied your route and controller into my environments (both 4.0 and 4.1) and everything works smoothyl with `/nest` url. Nothing wrong in those. – Simo A. Feb 09 '14 at 17:57
  • interesting. Could it be anything to do with my vhosts? Or did i not do something during install that enables routes to work? all these possibilities im pondering. Yes, the latest version i am running. – RSM Feb 09 '14 at 17:58
  • Yes, I would look into those, but unfortunately that's outside my knowledge. As I said, everything works perfectly in my env with your routes.php, controller and even .htaccess. My apache2 is running with all default settings, so nothing special is required there either. – Simo A. Feb 09 '14 at 18:05
  • I suggest you add a rewrite rule to prevent access to your .env file and other files in your root folder when to you use this method. Try accessing your .env file from your projects root like **http://your-project/.env** I suggest a rewrite rule like below: `RewriteRule ^(.*)env$ 404.php` – Nafiu Lawal Nov 09 '20 at 01:46

8 Answers8

10

Pretty URLs

The framework ships with a public/.htaccess file that is used to allow URLs without index.php. If you use Apache to serve your Laravel application, be sure to enable the mod_rewrite module.

If the .htaccess file that ships with Laravel does not work with your Apache installation, try this one:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

For more related help, check this answer.

Community
  • 1
  • 1
The Alpha
  • 131,979
  • 25
  • 271
  • 286
3

I enabled the mod_rewrite module in the Apache HTTP server, restarted Apache service and tested again and it WORKED!!!

Below is the code i used to enable mode_rewrite;

1) Un-Hash this line in Apache/conf/httpd.conf

LoadModule rewrite_module modules/mod_rewrite.so

2) Create a new configuration for "sample-project" without modifying the default configuration in the httpd.conf file

Directory "C:/Apache24/htdocs/sample-project">
  Options Indexes FollowSymLinks MultiViews
  AllowOverride All
  Order allow,deny
  Allow from all
Directory>
  • i used for ubuntu server cd /etc/apache2; sudo su; vim apache2.conf; and i added Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny Allow from all – Fadi Nov 11 '18 at 11:13
3

Be Alert! If it is not working even after enabling rewrite module. You need to change the line "AllowOverride None" to "AllowOverride All" in httpd.conf.

As @The Alpha alerted, Laravel don't need to be configured for this purpose.

2

When you change the line AllowOverride None to AllowOverride All in httpd.conf be sure that you do it inside <directory ...> that contains valid path.

Example

<Directory "/var/www/html"> #check path
   Options Indexes FollowSymLinks
   AllowOverride All
</Directory>
paper1111
  • 4,245
  • 2
  • 21
  • 40
Max Desmos
  • 21
  • 3
  • You are right, only apply the statement "AllowOverride All" in our specific web content directory such as in your code above, not in the section/block. It's mentioned clearly in the httpd.conf remark. – Lex Soft Nov 06 '19 at 15:18
2

For some reasons the answers above are not working for me, so here is the only one that worked for me.

I am using ubuntu 18.04 amd64 as my development server environment. So I modified apache.conf file located at/etc/apache2

From:

<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None Require all granted

To:

<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride All Require all granted

And Also From:

<Directory /usr/share> AllowOverride None Require all granted

To

<Directory /usr/share> AllowOverride All Require all granted

After that, you need to restart apache using sudo service apapche2 restart command in the terminal

Now I can access the page without using index.php in the URL

Credit goes to: joshymatheww @ Laracasts servers discussion channel on Aug 5, 2017

Wale
  • 1,307
  • 8
  • 25
2

Login to the terminal

sudo nano /etc/apache2/apache2.conf

After line

<Directory /var/www/>
.
.
.
</Directory>

and paste below code

<Directory /var/www/html/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
</Directory>

Now Ctrl+O and then Ctrl+M and then Ctrl+x

Now restart apache server

sudo service apache2 restart
David García Bodego
  • 1,042
  • 3
  • 9
  • 18
1

set this in your .htaccess

DirectoryIndex index.php

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]

RewriteRule .? %{ENV:BASE}/index.php [L]

Omid Ahmadyani
  • 1,012
  • 9
  • 13
0

Try the following .htaccess. I never had any trouble with that on a server or localhost environment.

DirectoryIndex index.php

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>

        RewriteEngine On

        RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
        RewriteRule ^(.*) - [E=BASE:%1]

        RewriteCond %{ENV:REDIRECT_STATUS} ^$
        RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

        RewriteCond %{REQUEST_FILENAME} -f
        RewriteRule .? - [L]

        RewriteRule .? %{ENV:BASE}/index.php [L]

    </IfModule>
Rashed Zaman
  • 350
  • 3
  • 7