.htaccess根据路径将子域重写到目录或文件

时间:2015-05-15 13:27:52

标签: regex apache .htaccess mod-rewrite

当我导航到sub.domain.com/path时,它会转到domain.com/site.php?site=sub&page=path

path类似于text.text时,我想将其指向domain.com/subdomains/sub/text.text,但我无法让它一起工作。

RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^(.*\..*)$ [NC]
RewriteRule ^(.*)$ site.php?site=%1&page=$1 [L]

path包含text.text时,应用此规则:

RewriteRule ^(.*)$ http://domain.com/subdomains/%1/$1 [P,L,NC,QSA]

另外,如何使上述规则使用相对路径(没有http://domain.com/)?

更新

sub.domain.com -> site.php?site=sub
sub.domain.com/path -> site.php?site=sub&page=path
sub.domain.com/path.ext -> subdomains/sub/path.ext

以上所有作品,仅剩一个:

sub.domain.com/constant/text.text -> constant/text.text

上面的那个应该只适用于常量是带点的文本。如果不适用此规则:

sub.domain.com/path -> site.php?site=sub&page=path 

2 个答案:

答案 0 :(得分:2)

您可以使用以下两条规则:

RewriteRule ^(constant|site\.php$) - [L,NC]

# path with no dot
RewriteCond %{HTTP_HOST} ^(.+?)\.domain\.com$ [NC]
RewriteRule ^/?$ site.php?site=%1 [L,QSA]

RewriteCond %{HTTP_HOST} ^(.+?)\.domain\.com$ [NC]
RewriteRule ^([^.]+)/?$ site.php?site=%1&page=$1 [L,QSA]

# anything else i.e. path with dot
RewriteCond %{HTTP_HOST} ^(.+?)\.domain\.com$ [NC]
RewriteRule ^(.+)$ http://domain.com/subdomains/%1/$1 [P,L,NC]

答案 1 :(得分:1)

您不能在一个规则中同时使用两个规则。如果你想使用相对URL提供子文件夹子域位于根目录中,那么使用Proxy标志也是如此。

long seed = new Random().nextLong();
Collections.shuffle(tmpWord, new Random(seed));
Collections.shuffle(tmpName, new Random(seed));