Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GregorSams's avatar

Laravel - Adapt .htaccess file to reach urls

I am using laravel 6 and run it on a apache/ubuntu 20 server.

My apache configuration looks like the following:

<VirtualHost *:80>

	ServerAdmin webmaster@localhost
	DocumentRoot /home/admin/Desktop/Code

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined


</VirtualHost>

As I am running laravel on a development machine I do not want to change my document root to point to the project.

My routes in my laravel app look like the following:

web.php

Route::get('/', 'IndexController@index');
Route::get('company/{symbol}', 'CompanyController@index');

In my .env file the APP_URL looks like the following:

APP_URL=http://localhost/laravel_app/public

I can reach my base url / the following way:

http://localhost/laravel_app/public/

When I go from / to another detail url f.ex. http://localhost/laravel_app/public/company/AAPL, I get:

enter image description here

My .htaccess file looks like the following:

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

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

I tried to setup a route like this Route::get('company', 'CompanyController@index'); and open http://localhost/laravel_app/public/company, but I didn't even jump with my debugger into my CompanyController.

Therefore I was thinking that my .htaccess file might be wrong.

Any suggestions how to adapt my .htaccess file to correctly open an url like /company/{symbol}?

I appreciate your replies!

0 likes
4 replies
jlrdw's avatar

You point to public as your document root, public shouldn't be in the url / uri.

The htaccess file that comes with laravel works properly already. You set this in your VH. Or use a second small htaccess to point to public.

In main folder:

    RewriteEngine On
    RewriteRule ^(.*)$ public/
    RewriteRule ^ server.php

in public folder, the one that ships with laravel:

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

    RewriteEngine On
    RewriteBase /laravel842/   I only added this one line.

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    # RewriteRule ^(.*)$ public/ [L]
</IfModule>

But the VH is better choice.

GregorSams's avatar

I do not want to change to DocumentRoot /home/admin/Desktop/Code/laravel_app/public as I am on a development environment.

Are there any other solutions to route laravel correctly?

GregorSams's avatar

Thx for your detailed response. Why did you change to laravel842. What is this?

jlrdw's avatar

That's the folder, doing that, I can leave off leading slash in a route or redirect:

Route::get('dog/add', 'DogController@add');

instead of

Route::get('/dog/add', 'DogController@add');

Please or to participate in this conversation.