16

I've setup a new install of Laravel on my local. It appears there are issues with htaccess or Apache settings. I've researched for a number of hours and tried everything I read.

  • OSX Lion 10.7.5
  • MAMP 3.0.5
  • PHP 5.5.10
  • mod_rewrite is being loaded.

My development server works with other sites. This is the first time I am trying Laravel 4.

I get a 403 Forbidden on the welcome page which is located at website.dev:8888/

Apache gives me this error: Directory index forbidden by Options directive

Here is my .htaccess file content:

<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>

Here are a few additional actions I've taken:

  • AllowOverride is set to All in httpd.conf
  • Added virtual host code section to httpd-vhosts.conf
  • verified that the hosts file contains a line for the site 127.0.0.1 website.dev

I've also tried various lines in the htaccess which I found in articles and I also restarted apache each time I made changes to the conf files. No routes work. When I go to website.dev:8888/public I get a blank page, no error. If I go to a route I created such as website.dev:8888/users I get a 404 not found error.

Thank you for your help!

PrestaShopDeveloper
  • 3,110
  • 3
  • 21
  • 30
Alexnl
  • 397
  • 1
  • 3
  • 12

9 Answers9

38

This solution worked fine, best solution ever for me. Paste this code into root htaccess. That's all. Leave all other files as they are

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

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]

RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
</IfModule>
Mohammad H.
  • 856
  • 9
  • 19
  • 2
    Where did this code originate? (How does this actually solve the OPs specific problems?) – MrWhite Mar 24 '20 at 00:03
  • Use this for root folder – Mohammad H. Mar 24 '20 at 15:55
  • 7
    Explanation for this solution would be appreciated, specifically parts which point redirection either to public or server -script, but it does seem to work. – Janne Nov 18 '20 at 13:58
  • First goes to public/* and then points to server.php. So no need to include /public/ anywhere else ;) – Mohammad H. Mar 25 '21 at 18:59
  • 1
    Anyway, thank you, it worked for me perfectly. Hope one day to understand the code, but for today, copy paste is ok – Juan Joya Aug 11 '21 at 04:36
  • @MohammadH. I do not have the problem of "/public" in the url. My only problem is that I have an unstyled app. My css js files are not loading. I am on laravel 6. I have tried all the solutions in this post. – Umair Jul 17 '22 at 10:23
  • this works for absolutes or relatives path, but will sometimes cause troubles when you have to deal with symbolik links – Abdulbasit Jul 27 '23 at 16:03
18

In the root path create a .htaccess file with

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_URI} !^/public/ 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f



RewriteRule ^(.*)$ /public/$1 
#RewriteRule ^ index.php [L]
RewriteRule ^(/)?$ public/index.php [L] 
</IfModule>

In public directory create a .htaccess file

<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>

if you have done any changes in public/index.php file correct them with the right path then your site will be live.

what will solve with this?

  • Hosting issue of laravel project.
  • Css not work on laravel.
  • Laravel site not work.
  • laravel site load with domain/public/index.php
  • laravel project does not redirect correctly
Rutvik Panchal
  • 199
  • 1
  • 3
6

Answer

Heres what I found that worked. Delete the .htaccess file inside the /public directory and then put the following into the laravel installation's document root.


Clarification

So for clarification if your laravel application is installed at /public_html/laravel you will look inside /public_html/laravel/public and delete the .htaccess file there. Then inside the /public_html/laravel directory make a new file called .htaccess


Copy the code below to a new .htaccess file in the doc root

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

    RewriteEngine On

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

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

Please note

As Concordus Applications noted in their answer here make sure that the Loadmodule mod_rewrite is uncommented (make sure there is not a # symbol behind it in your apache config file)

Concordus Applications also noted that you should have AllowOverride set to the appropriate setting. The example they gave was the following.

<Directory "/some/absolute/path/htdocs">
    Options Indexes Includes FollowSymLinks MultiViews
    AllowOverride AuthConfig FileInfo
    Order allow,deny
    Allow from all
</Directory> 

Please make sure to restart your apache server if you made any changes to your apache config file.

Community
  • 1
  • 1
Austin Kregel
  • 715
  • 1
  • 8
  • 27
5

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]
Cas Bloem
  • 4,846
  • 2
  • 24
  • 23
5

The accepted answer worked but not with laravel passport authentication.

There is a need to add some header tweaking:

RewriteEngine On

RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]

RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
Jonathan
  • 4,724
  • 7
  • 45
  • 65
4

This .htaccess will remove /public/ from your URL and force https://

Put this .htaccess in your root folder without renaming server.php file to index.php this .htaccess will do everything

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

    RewriteEngine On
    RewriteCond %{HTTPS} !=on    
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteRule ^(.*)$ public/$1 [L]

    # 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] 

</IfModule>

Pushkraj Jori
  • 187
  • 1
  • 12
  • If you get server errors, you might need to add the following to the .htaccess file right after "RewriteEngine On": "RewriteBase /" Works for me now on Ionos/1&1 webhosting. – ftw Dec 22 '21 at 17:53
  • @ftw I do not have the problem of "/public" in the url. My only problem is that I have an unstyled app. My css js files are not loading. I am on laravel 6. I have tried all the solutions in this post. – Umair Jul 17 '22 at 08:15
  • @umair Few things to check. 1. Check your APP_URL 2. Make sure you are using {{ asset('/css/app.css') }} some thing like this 3. Check your network tab to read what status codes you are getting. – Pushkraj Jori Jul 18 '22 at 13:59
  • @ftw Before I go with your solution, I did ctrl + u and this is what I saw. The /public is repeated twice in the url. See image https://i.postimg.cc/R00GQkXQ/Untitled.jpg – Umair Jul 19 '22 at 07:04
  • @ftw Here is the github repo https://github.com/cmate5614530/inventory-management – Umair Jul 19 '22 at 07:06
  • @ftw Where to put {{ asset('/css/app.css') }} by the way? – Umair Jul 19 '22 at 10:28
  • @ftw APP_URL is correct. In the network tab the statuses are 404. As for the /public appearing twice was because I was using ASSET_URL=public in .env file. What now? – Umair Jul 19 '22 at 11:07
  • @umair is this your actual repo? github.com/cmate5614530/inventory-management – Pushkraj Jori Jul 19 '22 at 14:46
  • @ftw No this is not my repo. I found where to make changes. resources\views\layout\main.blade.php I tried {{ asset('/css/app.css') }} but it is not working either. WHat else? – Umair Jul 19 '22 at 15:34
  • @PushkrajJori No this is not my repo. I found where to make changes. resources\views\layout\main.blade.php I tried {{ asset('/css/app.css') }} but it is not working either. WHat else? – Umair Jul 20 '22 at 08:09
  • Without seeing the code it's very hard to guess the issue. Create new public repo and upload your config folder, resources folder and public folder. and share the repo url here to get appraised. – Pushkraj Jori Jul 20 '22 at 16:13
3

Make sure you have .htaccess files in both /wwwroot and /public. The content is different though: wwwroot/.htaccess:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/ 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/$1 
#RewriteRule ^ index.php [L]
RewriteRule ^(/)?$ public/index.php [L] 
</IfModule>

wwwroot/public/.htaccess:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>
    RewriteEngine On    
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
RookieRoo
  • 161
  • 1
  • 6
  • This works for me in ionos hosting – meYnot Jan 08 '22 at 03:06
  • This works only with the default URL (localhost:8000) but not when calling other routes like this localhost:8000/Mycontroller. We need to include the public again to make these things work. – Mak-Mak Basaya Apr 25 '22 at 13:28
1

Create new .htaccess file in root folder. and put this:

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

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ ^$1 [N]

    RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
    RewriteRule ^(.*)$ public/$1

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ server.php
</IfModule>

and add this code in .htaccess at end in public/.htaccess file

    RewriteCond %{THE_REQUEST} \s/+(.+/)?public/(\S*) [NC]
    RewriteRule ^ /%1%2? [R=301,L,NE]
Darpan Patel
  • 387
  • 3
  • 15
0

Simple Solution Copy and Paste The Code Below Into Your .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase/
RewriteRule ^-$public/index.php[L]
RewriteRule ^((?!public/).*)$ public/$1 [L]
</IfModule>
Adam
  • 147
  • 1
  • 13