How to Set Document Root to Be a Subdirectory Using .Htaccess and Not Vhost

Apache2 - Set Document Root based on subdirectory

Is it possible to set Document Root for each of directories inside
main Virtual Host?

Short answer is no, you cannot. 1 Document Root per Virtual Host. In order to understand how the things are working you need to understand what is the directives context. Lets take as an example VirtualHost directive. As we can see from the docs it has only 1 Context which is Server Config. And server config context tells next:

directive may be used in the server
configuration files (e.g., httpd.conf), but not within any
VirtualHost or Directory containers. It is not allowed in
.htaccess files at all.

But DoocumentRoot has two contexts: server config and virtual host. Rest you should figure out for yourself ;)

There are plenty ways how you can achieve desired result

  1. Running several name-based web sites on a single IP address
  2. Running different sites on different ports
  3. Mixed port-based and ip-based virtual hosts

Hope that this answer will help you!

Using htaccess to change document root

add the following lines to the .htaccess file in the public_html folder:

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

HTACCESS: Change domain root to sub-directory

Try this: its a little crude, but should do what you want. There is a little bit of confusion: some of what I've tried to do will depend on your RewriteBase (you might need to remove one or more / characters).

I've basically added an initial block that specifically looks for directories within your /domain/ folder that don't end with a trailing slash and added one. Let me know if it works at all.

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond /domain/%{REQUEST_URI} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /domain/$1/

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domain/$1

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ /domain/index.php [L]

change Apache document root in XAMPP

You can get this work by setting apache virtual host at locally as describe below:

For example in ubuntu /etc/apache2/httpd.conf,
If you are using XAMPP on window that is the case, then the httpd.conf file should be C:\xampp\apache\conf\httpd.conf.

Please follow below steps to configure your local sites at local server:

------------
Step 1
------------
i.e
<VirtualHost *:80>
ServerName test.demo.tst ( Any name you want to set )
DocumentRoot "C:/xampp/htdocs/bf"
<Directory "C:/xampp/htdocs/bf">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
#RackEnv development
ErrorLog C:/xampp/htdocs/bf/logs/test.demo.tst_error
CustomLog C:/xampp/htdocs/bf/logs/test.demo.tst_access common
</VirtualHost>

------------
Step 2
------------
Than you need to restart apache service

------------
Step 3
------------
Than you need to do setting in your hosts files
for example in hosts file place below entry

127.0.0.1 test.demo.tst

------------
Step 4
------------
Than you can access this url test.demo.tst local server.

I hope this informations would be helpful to you.

Configure MAMP to treat each directory in Htdocs as Root in Apache mod_redirect

so that each virtual site gets it's own "root"?

You need to use VirtualDocumentRoot.

This is how I am using this on my MAMP in my /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf file:

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
ServerName localhost
ServerAlias *.localhost
VirtualDocumentRoot /Users/admin/htdocs/%0

RewriteLogLevel 3
RewriteLog "/Applications/MAMP/logs/rewrite.log"

<Directory /Users/admin/htdocs>
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

Take note of VirtualDocumentRoot /Users/admin/htdocs/%0 directive. That makes each virtual site's root as:

VirtualDocumentRoot /Users/admin/htdocs/localhost
VirtualDocumentRoot /Users/admin/htdocs/dev.localhost
VirtualDocumentRoot /Users/admin/htdocs/client2.localhost

etc.

Then simply create a directory within /Users/admin/htdocs/ for each site named as above, like:

dev.localhost
client2.localhost

Remove (or rename) any .htaccess files during the process - and once websites confirmed to be accessible via url like: http://client2.localhost, .htaccess files should behave as expected.

Also be sure that in the /etc/hosts file, there's an entry like:

127.0.0.1    client2.localhost

for each URL in question.



Related Topics



Leave a reply



Submit