Configure clean URLs for Laravel using a rewrite rule to index.php
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
Enabling of rewrite module
sudo a2enmod rewrite
I changed
AllowOverride None
toAllowOverride 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.
Related videos on Youtube
giannis christofakis
Updated on September 18, 2022Comments
-
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 cleanURLs
but the only thing I get are404 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 tohttp://laravel.lar/index.php/about/
I'm routing to theabout
page instead if I link tohttp://laravel.lar/about/
I get a404 Not Found
error.I'm using a Debian based system.
-
giannis christofakis almost 11 yearsNo, I've tried it doesn't work.
-
Stephen Ostermiller almost 11 yearsDoes
http://laravel.lar/index.php?/about/
give you a 404 error? -
giannis christofakis almost 11 yearsNope, I'm routing to
index.php
. -
Stephen Ostermiller almost 11 yearsI added another possibility to the answer.