简体   繁体   中英

Wildcard .htaccess rewrite subdomain to a subdirectory with subdomain as GET variable

Sample Urls:

http://username1.example.com/inventory/
http://username2.example.com/profile/about
http://example.com/products/category/

Rewrite to:

http://example.com/subdomains/inventory/?user=username1
http://example.com/subdomains/profile/about?user=username2
http://example.com/products/category/

I think I have searched most of the internet for this.

How do I rewrite the path by adding "subdomains" folder immediately after the domain and pass the subdomain as GET variable to access it via PHP? There shouldn't be any change in the url which the user can see in this address bar. There shouldn't be any change or rewrite when there is no sub domain.

Right now, I can get both path and sub domain as GET variables like below but that is not what I want.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^(.*)$ /index.php?subdomain=%1&request=$1 [L]
</IfModule>

Please consider that I am a .htaccess noob.

Try this (if you already have some other rewrite rules, put these on top):

RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
RewriteRule ^ http://example.com/subdomains%{REQUEST_URI}?user=%1 [QSA,L,P]

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