域映射的htaccess规则并保留当前配置

时间:2014-11-05 08:38:27

标签: regex apache .htaccess mod-rewrite dns

我有一个基于注册用户名的动态子域名网站,这些用户名是使用php解析的,因为我的htaccess没有问题。 以下是有关重定向如何从$ _SERVER

运行的示例

thecspace.com/blogs

["QUERY_STRING"]          => string(21) "template=www&q=blogs/"

http://www.thecspace.com/blogs/test-pp-blog-2904

["QUERY_STRING"]          => string(38) "template=www&q=blogs/test-pp-blog-2904"

http://danvlad.thecspace.com/blogs/test-pp-blog-2904

["QUERY_STRING"]          => string(47) "template=danvlad&q=blogs/test-pp-blog-2904"

当前的HTACCESS

RewriteCond %{HTTP_HOST} ^thecspace\.com$ [NC]
RewriteRule ^(.*)$ http://www.thecspace.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)?\.thecspace\.com
RewriteRule ^$ index.php?template=%2 [L,QSA]

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)?\.thecspace\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule (.*) index.php?template=%2&q=$1 [L,QSA]

现在来了困难的部分。我想允许自定义域使用相同的规则。让我们说devdemogroup有域devdemogroup.com。一旦他们将DNS更改为指向domain.com,我希望能够允许该域使用来自domain.com的当前代码

因此,理论上对www.danvlad.com的请求将是此查询字符串

["QUERY_STRING"]          => string(47) "template=danvlad"

和www.danvlad.com/blogs /

["QUERY_STRING"]          => string(47) "template=danvlad&q=blogs"

我已经尝试了这个但是它没有工作

RewriteCond   %{HTTP_HOST}   !^www\.thecspace\.com$ [NC]
RewriteCond   %{HTTP_HOST}   ^([^.]+)\.thecspace\.com$ [NC]
RewriteRule   ^$ /index.php?template=%1 [L]

RewriteCond   %{HTTP_HOST}   !^www\.thecspace\.com$ [NC]
RewriteCond   %{HTTP_HOST}   ^www\.([^.]+)\.thecspace\.com$ [NC]
RewriteRule   ^$ /index.php?template=%1 [L]

RewriteCond   %{HTTP_HOST}   !^www\.thecspace\.com$ [NC]
RewriteCond   %{HTTP_HOST}   ^www\.([^.]+)\.com$ [NC]
RewriteRule   ^$ /index.php?template=%1 [L]

RewriteRule   ^(.*)$ /index.php?template=%1?q=$1 [L,QSA]

有人可以帮忙吗? 谢谢

2 个答案:

答案 0 :(得分:1)

实际上你的上一个RewriteRule没有条件,请按照以下方式使用:

RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.thecspace\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?template=%1?q=$1 [L,QSA]

答案 1 :(得分:0)

最终解决方案

Route53 thecspace.com区域 danvlad .thecspace.com CNAME www.thecspace.com www.danvlad .thecspace.com CNAME www.thecspace.com

danvlad Zone www.danvlad.com CNAME www.thecspace.com

    选项FollowSymLinks     AllowOverride All     允许所有人     DirectoryIndex index.php

# Redirect thecspace.com and cspace.thecspace.com to www.thecspace.com

RewriteCond %{HTTP_HOST} ^thecspace\.com$ [NC] [OR]
RewriteCond %{HTTP_POST} ^cspace.thecspace.com$ [NC]
RewriteRule ^(.*)$ http://www.thecspace.com/$1 [R,L,QSA]

# Redirect abc.thecspace.com to www.thecspace.com/index.php?template=abc&q=zzz

RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.thecspace\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php?template=%1&q=$1 [L]

# Redirect www.abc.thecspace.com to www.thecspace.com/index.php?template=abc&q=zzz

RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.thecspace\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php?template=%1&q=$1 [L]

# Redirect www.abc.com to www.thecspace.com/index.php?template=abc&q=zzz

RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php?template=%1&q=$1 [L]

# Redirect www.thecspace.com

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME) !^$
RewriteRule ^(.*)$ /index.php?q=$1 [L]

也许对某人有所帮助。