带有子域

时间:2015-10-05 18:19:03

标签: apache .htaccess mod-rewrite

我一直在努力弄清楚这个htaccess问题,我似乎无法让它工作并且卡在500错误上。

我环顾了多个S.O.问题和一般谷歌但没有发现任何适用于我的特殊情况。这是:

我有example.com。我希望用户输入:example.comwww.example.com转到index.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等,所以想要在子域上捕获所有内容。希望这个问题有道理。我的Apache知识更有限,其中大部分来自Google搜索。

1 个答案:

答案 0 :(得分: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]