How to remove folder from url on a cPanel server using .htaccess

1.1k Views Asked by At

I have a domain and an hosting that allows me to have as many subdomain as i need using cPanel. I've created one called projects.test.com in which i upload all of my projects.

projects.test.com points to this specific path: public_html/projects

Now, my main domain, test.com, uses a 301 redirect to point to this path: public_html/home

and actually this bit is working, as if i type test.com, i'll be redirected to test.com/home.

Now, i have already write a question (here) on how to remove the /home from the url, and i though it was working. The only issue i've got with that, is that for some reason adding

RewriteEngine on
RewriteBase /

RewriteRule ^$ home/ [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!home/).+)$ home/$1 [L,NC]

on my root .htaccess, it break the sub domain projects.test.com.

So my question is, how can i change this to make it work only for the main domain without breaking this sub domain i currently have and others that i could create in future?

And also, do i need to edit the root (intended as public_html folder, or the one one level above public_html? Sorry but i'm new with all this type of server configuration and i'm still trying to learn.

Thanks for any advice

EDIT

The .htaccess file in my root is this:

<IfModule mod_deflate.c>
    SetOutputFilter DEFLATE
    <IfModule mod_setenvif.c>
        # Netscape 4.x has some problems...
        BrowserMatch ^Mozilla/4 gzip-only-text/html

        # Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip

        # MSIE masquerades as Netscape, but it is fine
        # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

        # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
        # the above regex won't work. You can use the following
        # workaround to get the desired effect:
        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

        # Don't compress images
        SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
    </IfModule>

    <IfModule mod_headers.c>
        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
</IfModule>

while the one in my public_html folder is:

RewriteOptions inherit

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !^/home
RewriteRule ^/(.*)$ /home$1 [L]

RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteRule ^/?$ "http\:\/\/www\.test\.com\/home" [R=301,L]
1

There are 1 best solutions below

1
Kyle Anderson On BEST ANSWER

cPanel lets you put an addon or subdomain outside of (or above) the public_html directory. Since you've already created the subdomain, login to cPanel and click on the Subdomains link. In the Modify a Subdomain section, you can edit the Document Root. You'll need to do that and set the path to something like this:

domains/projects.test.com

Doing this will keep all of your addon and subdomains organized in a single folder, outside of the public_html directory, and avoid a lot of headaches down the road. The next time you create an addon or subdomain, you will do the same thing and set the Document Root to domains/sub.test.com.

The absolute path to these addon and subdomains would be similar to this (except for the username and possibly the name of the home directory):

/home/<your_username>/domains/projects.test.com/

And, of course, your primary domain (the one associated with your account), will always point here:

/home/<your_username>/public_html/

With that issue resolved, if you want all requests sent to home but you don't want it to show up in the URL, your .htaccess file (in your public_html directory) should look like this:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html
</IfModule>

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript

    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

    <IfModule mod_setenvif.c>
        SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
    </IfModule>

    <IfModule mod_headers.c>
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
</IfModule>

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options +FollowSymLinks -MultiViews
    </IfModule>

    RewriteEngine On

    RewriteBase /

    RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
    RewriteRule .* - [L,R=405]

    RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
    RewriteRule ^/?$ http://www.test.com [R=301,L]

    RewriteCond %{HTTP_HOST} ^www\.test\.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/home/
    RewriteRule ^(.*)$ /home/$1 [L]
</IfModule>

You also mentioned that you have an .htaccess file in your root directory, which I assume you mean here:

/home/<your_username>/.htaccess

Apache (or whatever HTTP server your host runs) will not read that file. Only files in your public_html directory (or other document root directories) are read.