Search code examples
laravelapache.htaccessmod-rewrite

Laravel remove public from url on localhost


On production I have domain http://xxxx.com. In root folder I added .htaccess:

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

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ /public/$1 [L,QSA]
</IfModule>

And when I type in the browser: http://xxxx.com it works.

The same .htaccess file I have on my machine. I use XAMPP. And it doesn't work. When I type:

http://localhost:8082/myfolder 

I get 404. When I type:

http://localhost:8082/myfolder/public

It works. What is the difference between url in my hosting and on my local machine?


For the first answer: this is a screen, maybe I'm doing something wrong (.htaccess is in the root folder), maybe .htaccess in the public folder is wrong: rewrite url htaccess


Solution

  • This should work for you:

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews -Indexes
        </IfModule>
    
        RewriteEngine On
    
        RewriteRule !^public/ public%{REQUEST_URI} [L,NC]
    </IfModule>
    

    The problem with your rule is that you're checking this condition:

    RewriteCond %{REQUEST_URI} !^/public/
    

    Which actually checks for URI /public from web root however you have your site inside a subfolder locally. Here RewriteRule !^public/ will check if public/ is not there at the start after the current directory context, which will work the same in site root as well in a subdirectory.

    Similarly for rewriting also make sure you don't use / before public/ to allow it to use a relative path.