I have a little problem with my .htaccess. For example, if I go to test.example.com, it's in folder /sub/test. But it shows test.example.com/sub/test. How can I hide the path after .com? Or is there any way to do it with Apache, like all folders in sub folder are subdomains?

I don't know how to do it.

I'm using this:

RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.example\.com$ [NC]
RewriteRule ^((?!sub/).*)$ /sub/%1/$1 [L]

EDIT: I want to make every created folder in sub folder as a subdomain automatically. Like user create folder and it's subdomain.

  • So, your subdomain's document root points to the root directory? And this is simply a wildcard subdomain I assume? Or have you configured this subdomain using a hosting control panel of some kind (eg. cPanel) and perhaps incorrectly set this as a "redirect"? The directives you posted won't do this by themselves. Do you have other directives in your .htaccess file? Any other .htaccess files? Check the network traffic, presumably you are seeing a 3xx redirect? What type?
    – MrWhite
    Nov 28, 2017 at 9:51
  • htaccess isn't the right tool for the job. You should move the directory structure and adjust your virtual hosts. Nov 28, 2017 at 11:44

1 Answer 1


Remove your rules and just have something like:

<VirtualHost IP>
    ServerName test.example.com
    DocumentRoot /whatever/sub/test

(replace whatever by the correct path, and IP by the relevant IP/port based on how your virtualhosts are configured)

For dynamic virtual host have a look at module mod_vhost_alias http://httpd.apache.org/docs/2.4/mod/mod_vhost_alias.html), quoting from documentation:

For simple name-based virtual hosts you might use the 
following directives in your server configuration file:

UseCanonicalName    Off
VirtualDocumentRoot "/usr/local/apache/vhosts/%0"

A request for http://www.example.com/directory/file.html will be 
satisfied by the file

Otherwise, for your rewrite rules, it may work just by adding [P] as flag, but as a general rule I always recommend to find simpler ways to do things and use rewriting only if things can not be done otherwise (due to the complexity it adds).

  • 1
    Yes, but I want to make every created folder in sub folder as domain automatic. Like user create folder and it's subdomain without adding new virtualhost.
    – Oggy mot
    Nov 28, 2017 at 12:46
  • Please have a look at my answer: mod_vhost_alias permits you to do exactly what you describe. You configure it just once. Nov 28, 2017 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.