Rewrite folder to subdomain with .htaccess?

2019-06-11 06:42发布

问题:

I'm currently building a large scale website that allows each profile page to be it's own unique sub-domain. I was wondering how would I do this using an .htaccess and mod_rewrite?

For example:

Turn this: http://example.com/profile/userid into: http://userid.example.com

回答1:

You can try this, if you are using wildcard DNS:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.example\.com$
RewriteRule ^(.*)$ profile/%1/$1 [QSA]