laravel.htaccesscpanelpublic-html

How .htaccess works for public_html cpanel


I have a Laravel project uploaded in root: home/user/my-project-directory and in public_html i have a directory some-directory-name. In this directory, I have a public Laravel directory.

The part that I don't understand is, where should I have a .htaccess file? In public_html directory or in every directory from public_html?

At this moment, when I go to www.mydomain.ro I get an index of public_html directory. After I select some-directory-name, I can access my website.

Why I'm asking this... If I have a .htaccess file in public_html and a .htaccess file in public_html/some-directory-name or if I don't have any one of them, the website still works the same.


Solution

  • The part that i don't understand is, where should i have a .htaccess file. In public_html directory or in every directory from public_html?

    It depends what you are trying to achieve. But it is very common to have just one .htaccess file in the document root of your site. The .htaccess file in the document root can control the entire directory tree. The .htaccess file in the subdirectory overrides the parent .htaccess file and controls just that branch of the directory tree.

    If you wanted to hide/rewrite the some-directory-name in the URL-path then you can't do this using only the .htaccess file in some-directory-name subdirectory, you would need to use the .htaccess file in the parent directory.

    If you have a .htaccess file in every directory then it can get very messy and difficult to maintain. But there can be reasons to do this (but you don't have to).

    You can also put the .htaccess file above the document root - but this may not do anything, depending on your server config.

    If i have a .htaccess file in public_html and a .htaccess file in public_html/some-directory-name or if i don't have any one of them, the website still works the same.

    Then maybe you don't need a .htaccess file. However, how are you going to route "pretty" URLs through Laraval if you don't have a .htaccess file?

    Incidentally, if you have access to the server config then you don't need to use .htaccess at all.

    Aside: Why do you have a directory structure (public URL-path) of the form /some-directory-name/public/? Is that intentional?


    UPDATE:

    What i'm trying to do is to access my website via www.mydomain.ro, not www.mydomain.ro/some-dir-name

    In that case, you would need to use the .htaccess file in the document root to internally rewrite all requests to the /some-dir-name subdirectory. For example:

    RewriteEngine On
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^ /some-dir-name%{REQUEST_URI} [L]
    

    You also need to make sure that all URLs in your project do not include the /public-dir-name URL-path.

    This also renders all URLs outside the /public-dir-name directory inaccessible. If you specifically need to access these files then additional conditions can be added.

    The condition that checks the REDIRECT_STATUS environment variable is to ensure against a rewrite loop.

    UPDATE#2: If you specifically need this to work for just one hostname, eg. example.com (and exclude all other hostnames / subdomains) then add an additional condition:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example\.com [NC]
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^ /some-dir-name%{REQUEST_URI} [L]
    

    UPDATE#3: To make it work for a subdomain as well then you can modify the CondPattern to make the subdomain optional. For example:

    RewriteCond %{HTTP_HOST} ^(subdomain\.)?example\.com [NC]
    

    Or, add an additional condition and use the OR flag. For example:

    RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
    RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]