Question

Problem pointing domain to subdirectory in .htaccess

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.


Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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