仅当url中存在子域时才进行路由

时间:2014-01-07 17:16:53

标签: php .htaccess

如果子域名在url:

中,我想这样做
http://example.com/                 -> index.php   
http://hello.example.com/           -> profile.php?subdomain=hello  
http://whatever.example.com/        -> profile.php?subdomain=whatever  
http://example.com/world            -> world.php   
http://example.com/world/test       -> world.php?id=test
http://example.com/sth              -> sth.php   
http://example.com/sth/test2        -> sth.php?id=test2
http://hello.example.com/world/test -> profile.php?subdomain=hello 

其他文件:

RewriteRule ^dodaj_artykul dodaj_artykul.php [L]
RewriteRule ^logowanie logowanie.php [L]
RewriteRule ^rejestracja rejestracja.php [L]
RewriteRule ^wyloguj wyloguj.php [L]


RewriteRule ^kategorie/([^-]+)$ kategorie.php?id=$1 [L]
RewriteRule ^kategorie/$ kategorie.php [L]
RewriteRule ^kategorie$ kategorie.php [L]

如果存在子域,则所有内容都将路由到“profile.php?subdomain = hello” 如果不存在正常“RewriteRule ^ kategorie /([^ - ] +)$ kategorie.php?id = $ 1 [L]”

1 个答案:

答案 0 :(得分:1)

将此项添加到您的其他规则之上:

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