Use .htaccess to rewrite any subdomain to the corresponding subdir without revealing the subdir
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about domains, htaccess, subdomain, , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :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.
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
/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).
Comments
Post a Comment