[Apache] Rewrite subdomain as subfolder

What You Want:

/path/to/www/root/
—————–/123/index.php ===> http://123.yourwebsite.com/
—————–/www/index.php ===> http://www.yourwebsite.com/
—————–/blog/index.php ===> http://blog.yourwebsite.com/
—————–/email/main/index.php ===> http://email.yourwebsite.com/main/

Set dns:

A (may be AAAA) * to your server’s IP address.

This is the rewrite code you should use:

RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond %{HTTP_HOST} ^(.*)\.yourwebsite\.com$
RewriteRule ^(.*)$     /path/to/www/root/${lowercase:%1}/$1 [L]

PS:

Options -Indexes

Will not working in this case. Since Apache can not find the subfolder if you hide them. All subdomains will return a 404 error.

One thought on “[Apache] Rewrite subdomain as subfolder”

  1. apache – Redirect wildcard subdomains to subdirectory, without changing URL in address bar – Stack Overflow

Comments are closed.