Home > Back-end >  htaccess Mask subfolder as root
htaccess Mask subfolder as root

Time:10-24

I have a website on the root of my domain and I need to show everything from the root folder masked as a subfolder.

Example.

When I open domain.com/test/ I need to show the same content as domain.com

When I open domain.com/test/option1/ I need to show the same content as domain.com/option1/

I've tried multiple htaccess options but I couldn't make it work.

Is this possible? Thanks

UPDATE

I added this to the .htacces file inside test folder

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$
RewriteRule ^/?(.*)$ test/$1 [L]

CodePudding user response:

Your setup is wrong in two aspects:

  1. the location of the distributed configuration file (".htaccess"), you need to place it in the DOCUMENT_ROOT folder of that http host, not inside the /test folder.

  2. You need to implement the rewriting logic the other way round:

This should do what you want to achieve:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^/?test/(.*)$ /$1 [L]

You should generally prefer to implement such rules in the actual http server's host configuration. If you do not have access to that (for example if you are using a cheap hosting provider), then you can indeed use a distributed configuration file. The rule is implemented such that it will work likewise.

Whether the condition you implemented is actually required depends on your setup: if you have multiple http hosts your server offers then this might be required if the hosts have different logic. Otherwise you can simply remove it.

  • Related