简体   繁体   中英

Use .htaccess to redirect virtual subdomains except for www

I'm already using http.d to drive (via 301 redirect) all non-www traffic to the www version of the site. Ie example.com redirects to www.example.com.

This works perfectly.

I would now like to allow users to set up virtual subdomains. So, if somebody visits bob.example.com, I'd like mod_rewrite to actually serve the page www.example.com/page.php?user=bob.

Since all of my Google history is set up to index the www version of the site, I don't want to lose that, so I want www excluded.

Thanks for your help.

You can use this rule in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.example\.com$ [NC]
RewriteRule ^ page.php?user=%1 [L,QSA]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM