2015-10-05 86 views
1

我一直想弄清楚这个htaccess的问题,我似乎无法得到它的工作,并陷入了500错误。mod_write htaccess与子域名

我看了看周围多个S.O.问题和一般谷歌,但没有发现任何适用于我的特定情况。这里是:

我有example.com。我想如果用户放入:example.comwww.example.comindex.php

但是,如果用户放在一个子域subdomain.example.com;我想它去

dashboard.php?val=subdomain 

进一步,我想

subdomain.example.com/contact 

dashboard.php?val=subdomain&page=contact 

这是我到目前为止有:

RewriteEngine On 
RewriteBase/

# If doesn't start with www and it has a subdomain, redirect 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC] 
RewriteRule .? - [E=VAL:%1] 
RewriteRule ^/?$ /dashboard.php?val=%{ENV:VAL}&page=idx 
RewriteRule ^contact/?$ /dashboard.php?val=%{ENV:VAL}&page=contact [L] 
#Note: I have tried this with and without the Env variables and only having 1 RewriteRule 

# The "catch all other" redirect for images, files and if they do /cont by accident 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC] 
RewriteCond %{REQUEST_URI} !dashboard\.php [NC] 
RewriteRule . /dashboard.php?val=%1&page=idx - [L] 

有可能是subdomainA和subdomainB等,所以想做一个catch全部在子域上。希望这个问题是有道理的。我的Apache知识更加有限,其中大部分来自Google搜索。

回答

0

给下面的规则一试:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.example\.com$ [NC] 
RewriteRule ^/?$ /dashboard.php?val=%1 [NC,L] 

RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.example\.com$ [NC] 
RewriteCond %{REQUEST_URI} !^/dashboard\.php [NC] 
RewriteRule ^.*$ /dashboard.php?val=%1&page=$0 [NC,L]