2

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.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Ioan Andrei
  • 93
  • 1
  • 16

1 Answers1

2

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]
DocRoot
  • 1,176
  • 10
  • 17
  • What i'm trying to do is to access my website via www.mydomain.ro, not www.mydomain.ro/public-dir-name and i think i have a .htaccess problem. My structure is this: Home(root dir)/my-project-dir(all laravel app files without public dir) Home/public_html/some-dir-name(all public folder from laravel app) In this moment, to access the website i need to go like this: www.mydomain.ro/some-dir-name. I'm trying to get rid of /some-dir-name – Ioan Andrei Oct 29 '18 at 11:52
  • And if i do this, my subdomains will work or all the URLs will point to www.mydomain.ro? – Ioan Andrei Oct 29 '18 at 13:59
  • Just to make it clear....Do i need to edit the home/user/public_html/some-dir-name or do i need to create a .htaccess file in home/user/project-dir with your code in it? – Ioan Andrei Oct 29 '18 at 14:04
  • It depends where your subdomains point to on the filesystem, but it is quite possible that this would catch all your subdomains as well. However, you can add an additional condition to rewrite just the domain(s) you want. I've updated my answer. You should only need to edit the `.htaccess` file in your document root (presumably your `public_html` directory). I assume you already have a `.htaccess` file in the Laravel root directory. – DocRoot Oct 29 '18 at 17:35
  • You can modify the _CondPattern_ (the second argument to the `RewriteCond` directive is a regular expression). I've updated my answer. – DocRoot Oct 29 '18 at 18:09
  • Thank you.I will try and return with a comment:) One more thing.Should i delete the default htaccess or add your code after the default code ends? – Ioan Andrei Oct 29 '18 at 18:19
  • That depends what the "default code" is. You probably want to put these directive _before_ any existing directives in the document root, otherwise they may not do anything. – DocRoot Oct 29 '18 at 18:29