Mobile app version of vmapp.org
Login or Join
Mendez628

: Use .htaccess to rewrite any subdomain to the corresponding subdir without revealing the subdir I have a little problem with my .htaccess. For example, if I go to test.example.com, it's in folder

@Mendez628

Posted in: #Domains #Htaccess #Subdomain

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/ [L]


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

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Mendez628

1 Comments

Sorted by latest first Latest Oldest Best

 

@Merenda212

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 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 www.example.com/directory/file.html will be
satisfied by the file
/usr/local/apache/vhosts/www.example.com/directory/file.html.


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).

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme