Report this

What is the reason for this report?

Problem pointing domain to subdirectory in .htaccess

Posted on September 20, 2020

I created a project based on laravel api and nuxt (pre-rendered), where the backend (api), inside root is served via api.domain.com (subdomain) and nuxt front-end, inside a separate folder root/client/dist, is served via domain.com (primary domain). I created the following .htaccess file at the root:

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

    RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
    RewriteCond %{REQUEST_URI} !^/client/dist/

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /client/dist/$1

    # Handle Front Controller...
    RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
    RewriteRule ^(/)?$ client/dist/index.html [L]

    //FOR API REQUESTS (LARAVEL)
    RewriteCond %{HTTP:Host} ^(?:api\.domain\.com)?$
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*) public/$1 [NC,L,NS]

</IfModule>

The urls domain.com and api.domain.com work fine. However, when I access domain.com/products, it redirects to domain.com/client/dist/products/, resulting in 404 Not Found error. However, when I access the same url from homepage, it renders correctly without any redirection.

Is there any way I can solve this? I tested it with nginx sites-enabled and it works fine.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi @sdriyaz712,

I believe your issue is with the following redirection rule

   RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
    RewriteCond %{REQUEST_URI} !^/client/dist/

You should add !^/products as a condition to it as well.

   RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
   RewriteCond %{REQUEST_URI} !^/client/dist/
   RewriteCond %{REQUEST_URI} !^/products/

Regards, KFSys

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.