Problem pointing domain to subdirectory in .htaccess

Posted September 20, 2020 3.4k views
ApacheLAMP StackDeploymentInitial Server SetupVue.jsLaravel

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

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

    RewriteEngine On

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

    RewriteCond %{HTTP_HOST} ^(www.)?$
    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.)?$
    RewriteRule ^(/)?$ client/dist/index.html [L]

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


The urls and work fine. However, when I access, it redirects to, 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.

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.

Submit an Answer
1 answer

Hi @sdriyaz712,

I believe your issue is with the following redirection rule

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

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

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