Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Mayur Pande
PLUS
Mayur Pande
Courses Plus Student 11,711 Points

How to write htaccess file for subfolder in /var/www/html directory

How would I go about re-writing the htaccess code below so that I could put my site in a subfolder of /var/www/html as at the moment I have all my files and directories for one site in the root /var/www/html

Here is my htaccess file code

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

and here is what inside my main directory and what is in it looks like (d - for directory, f - file)

/var/www/html (d)

   -views(d)
   -scripts(d)
  -stylesheets(d)
  -index.php(f)
  -vendor(d)
  -.htaccess(f)
  -composer.json(f)
  -src(d)

but what I want to achieve is to put all of those directories/files into a sub-directory so then eventually I can have multiple sites within different sub-directories like so;

/var/www/html (d)

   -website1(d)(where this contains all the files and directories from above)
   -website2(d)(other new website with different files and folders)
  -website2(d)(other new website with different files and folders)
  -website2(d)(other new website with different files and folders)

For my initial project (website1) I would have to rewrite the htaccess file above, but I am not sure how to go about this?

2 Answers

Codin - Codesmite
Codin - Codesmite
8,600 Points

That sounds more like the paths for your images and stylesheets are incorrect once moving the website into a subfolder?

So for example where before you may have had the following based off of the root:

<link rel="stylesheet" type="text/css" href="/styles/global.css">

It would need to be changed incude to:

<link rel="stylesheet" type="text/css" href="/tutor/styles/global.css">

or relative to the current position of the file that is including the stylesheet/image:

<link rel="stylesheet" type="text/css" href="styles/global.css">
Codin - Codesmite
Codin - Codesmite
8,600 Points

I am unsure why you would require to do this in htaccess?

Would it not be easier to just drop the websites into subfolders on the server?

Like this:

-website1(d)
   -scripts(d)
      -script.js(d)
   -src(d)
   -stylesheets(d)
      -styles.css(f)
   -views(d)
   -vendor(d)
   -.htaccess(f)
   -composer.json(f)
   -index.php(f)

-website2(d)
   -scripts(d)
      -script.js(d)
   -src(d)
   -stylesheets(d)
      -styles.css(f)
   -views(d)
   -vendor(d)
   -.htaccess(f)
   -composer.json(f)
   -index.php(f)

-website3(d)
   -scripts(d)
      -script.js(d)
   -src(d)
   -stylesheets(d)
      -styles.css(f)
   -views(d)
   -vendor(d)
   -.htaccess(f)
   -composer.json(f)
   -index.php(f)

-website4(d)
   -scripts(d)
      -script.js(d)
   -src(d)
   -stylesheets(d)
      -styles.css(f)
   -views(d)
   -vendor(d)
   -.htaccess(f)
   -composer.json(f)
   -index.php(f)

This would seem to be the easiest solution unless I am not understanding your question?

Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

Thanks for the answer unfortunately this doesn't work. At the moment all of my websites I am working on need to be in /var/www/html ** so if I try to create a subfolder in that called **Tutor so the url will be for one of the pages /var/www/html/Tutor/contact-us instead of /var/www/html/contact-us the htaccess file within the Tutor folder messes up the layout and vendor files as I only get back the home page with no images or stylesheets applied and if I try to navigate to another page it gives me a 404 error. I think this is due to the htaccess file?

Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

sorry I was being an idiot! I just figured out its because my .htaccess file did not have the line

RewriteCond %{REQUEST_FILENAME} !-d

in it, in order to allow for directories so this

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

becomes this;

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

however I will have to either remove this line when uploading to the web as I have directories that I do not want people to look into or set permissions on them so others can't access them.