Configure clean URLs for Laravel using a rewrite rule to index.php

42,539

Solution 1

I think I see your problem. You are adding an extra question mark in your rewrite rule.

RewriteRule ^(.*)$ index.php?/$1 [L]

Should be

RewriteRule ^(.*)$ index.php/$1 [L]

based on the example urls your provide. http://laravel.lar/index.php/about/ doesn't have a question mark in it after index.php

It is also possible that mod_rewrite is not enabled. It looks like you are an a Debian based system such Ubuntu. You can use the command sudo a2enmod rewrite to enable mod_rewrite.

Solution 2

I met the same problem today. I solved it in two steps

  1. Enabling of rewrite module

    sudo a2enmod rewrite
    
  2. I changed AllowOverride None to AllowOverride All in my virtual host configuration file. AllowOverride None - prevents Apache server from reading of any .htaccess file located in your site directories.

Hope it helps.

Share:
42,539

Related videos on Youtube

giannis christofakis
Author by

giannis christofakis

Updated on September 18, 2022

Comments

  • giannis christofakis
    giannis christofakis over 1 year

    Recently I've started learning Laravel , I have none experience with framework before. I'm encountering the following problem .I'm trying to configure the .htaccess file so I can have clean URLs but the only thing I get are 404 Not Found error pages. I have created a virtual host - you can see below the configuration file - and changed the .htaccesss file on the public directory.

    /etc/apache2/sites-available

    <VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName  laravel.lar
    
        DocumentRoot "/home/giannis/Desktop/laravel/public"
        <Directory "/home/giannis/Desktop/laravel/public">
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
        </Directory>
    
        <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
        </Directory>
    
        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
            AllowOverride None
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Order allow,deny
            Allow from all
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
    
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
    
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        Alias /doc/ "/usr/share/doc/"
        <Directory "/usr/share/doc/">
            Options Indexes MultiViews FollowSymLinks
            AllowOverride None
            Order deny,allow
            Deny from all
            Allow from 127.0.0.0/255.0.0.0 ::1/128
        </Directory>
    
    </VirtualHost>
    

    .htaccesss file:

    laravel/public

    # Apache configuration file
    # http://httpd.apache.org/docs/current/mod/quickreference.html
    
    # Note: ".htaccess" files are an overhead for each request. This logic should
    # be placed in your Apache config whenever possible.
    # http://httpd.apache.org/docs/current/howto/htaccess.html
    
    # Turning on the rewrite engine is necessary for the following rules and
    # features. "+FollowSymLinks" must be enabled for this to work symbolically.
    
    
    
    <IfModule mod_rewrite.c>
        Options +FollowSymLinks
        RewriteEngine On
    </IfModule>
    
    # For all files not found in the file system, reroute the request to the
    # "index.php" front controller, keeping the query string intact
    
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?/$1 [L]
    </IfModule>
    

    In order to test it, I have created a view named about and made the proper routing. If I link to http://laravel.lar/index.php/about/ I'm routing to the about page instead if I link to http://laravel.lar/about/ I get a 404 Not Found error.

    I'm using a Debian based system.

  • giannis christofakis
    giannis christofakis almost 11 years
    No, I've tried it doesn't work.
  • Stephen Ostermiller
    Stephen Ostermiller almost 11 years
    Does http://laravel.lar/index.php?/about/ give you a 404 error?
  • giannis christofakis
    giannis christofakis almost 11 years
    Nope, I'm routing to index.php .
  • Stephen Ostermiller
    Stephen Ostermiller almost 11 years
    I added another possibility to the answer.