<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    RewriteBase /

    RewriteRule ^/?(.*)\.(env) - [F]

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

    # Redirect Home when direct access to Laravel Index page
    RewriteRule ^index\.php(/.*)?$ / [L,R=301]

    # Rewrite for Sharing
    RewriteCond %{REQUEST_URI} !/(sharing|api|storage|sitemap)/
    RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit|twitterbot|telegrambot|whatsapp|linkedinbot|duckduckbot|yandexbot|baiduspider|slurp|exabot) [NC]
    RewriteRule ^(.*)$ sharing/$1 [L,NC]

    # Rewrite for Backend
    RewriteRule ^(api|oauth|sitemap|sharing)/(.*)$ public/index.php/$1/$2 [L,NC]

    RewriteRule ^storage/(.*)$ public/storage/$1 [L,NC]

    # Rewrite for AdminCP
    RewriteRule ^admincp/(.*)$ public/web/admincp/$1 [L,NC]
    RewriteRule ^admincp$ public/web/admincp/ [L,NC]

    # Rewrite for Installation
    RewriteRule ^install$   install/ [L,NC,R=301]
    RewriteRule ^install/$  public/install/index.html [L,NC]

    RewriteCond %{REQUEST_URI} ^/public/web/
    RewriteCond %{REQUEST_URI} !^/public/web/admincp/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ public/web/index.html [L]

    RewriteCond %{REQUEST_URI} ^/public/web/admincp/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*)$ public/web/admincp/index.html [L]

    # Default to Frontend
    RewriteCond %{REQUEST_URI} !^/public/(web|install|storage)/
    RewriteCond %{REQUEST_URI} !(.+)\.php$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^public/(.*)$ public/web/$1 [L]

    # Redirect all requests to public folder
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
