1

I am trying to create an .htaccess file that will change the document root of a domain to a subfolder on my server without actually changing the url to the end user. Here is an example of what I am trying to do:

  • domain1.com will look for web content in /domains/domain1.com but still appear as domain1.com.
  • domain2.com will look for web content in /domains/domain2.com but still appear as domain2.com.

Every domain will be pointed to the root directory / so I am placing the .htaccess file here, /.htaccess.

I do not want to have to add every domain I have to the .htaccess file, I would like it to follow this format for every domain that comes its way.


I tried learning about .htaccess and create one myself, however, I can't seem a way to this. What I have currently have makes each domain redirect to its subdirectory and gives an error of "too many redirects".

  • ie. domain1.com redirects the end user to domain1.com/domains/domain1.com

This is the contents of the .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^(.*)$ /domains/%1 [L]
Tom
  • 1,223
  • 1
  • 16
  • 27

1 Answers1

0

Not bad, try this:

RewriteEngine on
# remove www, externally for consistent domains
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule .* http://%1/$0 [NS,L,R=301]
# document root emulation
RewriteCond %{HTTP_HOST} .+
RewriteRule ^(?!domains/[^/]+/).* domains/%0/$0 [NS,L]
Walf
  • 8,535
  • 2
  • 44
  • 59