Search code examples
.htaccesscpanelcreate-react-app

Change document root to subfolder in cpanel


I have a React app created with Create React App. I need to sever the files from /build/. How to change to root to /build? I have already pushed the entire files ie src, public and this is with git. If I had known, id git only the build folder but that seems too late (deploybot, bitbucket etc).

Ive created a .htaccess file with

RewriteEngine on
RewriteCond %{HTTP_HOST} ^web.com/$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.web.com/$
RewriteCond %{REQUEST_URI} !build/
RewriteRule (.*) /build/$1 [L]

But nothing works when I got to /. All that I see are the build folder, src etc


Solution

  • You can use this rule:

    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^(?:www\.)?web\.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/build/
    RewriteRule (.*) /build/$1 [L]
    

    In your condition:

    RewriteCond %{HTTP_HOST} ^web.com/$ [NC,OR]
    

    There is a trailing slash which is never part of HTTP_HOST variable and there is an an unescaped dot that may match any character.

    You can also combine both conditions into one by making www. optional as in my answer.