Skip to content

Instantly share code, notes, and snippets.

@liaotzukai
Last active September 6, 2023 11:39
Show Gist options
  • Star 57 You must be signed in to star a gist
  • Fork 29 You must be signed in to fork a gist
  • Save liaotzukai/8e61a3f6dd82c267e05270b505eb6d5a to your computer and use it in GitHub Desktop.
Save liaotzukai/8e61a3f6dd82c267e05270b505eb6d5a to your computer and use it in GitHub Desktop.
<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>
@guilhermemuller
Copy link

It worked for me, thank you very much.

@mrhili
Copy link

mrhili commented Aug 26, 2018

Worked for me to thank you

@HeroGuys
Copy link

HeroGuys commented Sep 5, 2018

My laravel does not see css, js, etc files and routes
Access file in / public

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

Access file in / root folder laravel project

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

@BassemN
Copy link

BassemN commented Sep 21, 2018

Thanks allot 👍

@BassemN
Copy link

BassemN commented Sep 21, 2018

@liaotzukai Could you explain what each rule do, please?

@Akki-8388
Copy link

Its working thank you,it saved my time @HeroGuys

@sibche2013
Copy link

hello
@liaotzukai this code works very well , but when we can access site with site.com and site.com/public ! how can we disable view in public folder? i want only user can view site from site.com URL not site.com and site.com/public

@nandalal
Copy link

Hello
@liaotzukai everything is working fine, when i past the url with /public also able to access the normal way. is it any issues, and i need to block the .env file, so i added one more line to the above .htaccess. on the bottom line of " RewriteRule ^ server.php ". i added " RewriteRule ^.env - [F,L,NC] ".
Can you tell me the solution for block the link if we put additional uri called /public to the normal url

@moashraf
Copy link

moashraf commented Nov 4, 2019

My laravel does not see css, js, etc files and routes

@Kashiffrq
Copy link

Great resolve my issue
Thanks

@mantir
Copy link

mantir commented Feb 5, 2020

I love you. Sincerely. How much time I have wasted before I found this.

@dikiwidia
Copy link

Thank you very much. its work !

@jalemboh
Copy link

jalemboh commented May 1, 2020

@HeroGuys : Thanks

@carltheraveyt
Copy link

causes a 500 error on namecheap

@imvikaskumar
Copy link

@HeroGuys Thanks a lot, it's working perfectly

@Wanuja97
Copy link

Wanuja97 commented Oct 6, 2021

@HeroGuys It's working, thanks a lot.

@cdsaenz
Copy link

cdsaenz commented Mar 19, 2022

Seems to work great in Laravel 8. The only one I found! Thanks!

@josefernandoperezgarcia1998

THIS WORKS FOR ME IM USING LARAVEL 5.6

1.- I CREATE A NEW FILE IN MY ROOT DIRECTORY OF MY PROJECT CALL IT ".htaccess"

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

2.- I pasted this piece of code on my new file, refresh the browser and thats it.

Whats the problem: I had the problem: subdomain.domain.com/name-project/public/

The result I want ♥: And it woks! I have the solution The result: subdomain.domain.com/name-project/

Thanks @liaotzukai

@sitemapxml
Copy link

sitemapxml commented Jun 20, 2022

@liaotzukai Do you know how this code would look like for OpenLiteSpeed web server?
There are many guides that recommend enabling .htaccess support inside OLWS configuration, but I would like to avoid using access files, and add the rules directly inside virtual host config file.
There are not many online guides regarding Laravel and OLWS...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment