htaccess通过删除点从多个子域重定向

时间:2017-05-11 23:50:04

标签: php apache .htaccess redirect cakephp

我有一组子子域,我需要在.htaccess中重定向。我不擅长REGEX,并且未能理解我已经阅读的其他帖子,以便将其应用于这种情况。

我的网站布局如下:

quark.subdomain.site.com
yy.subdomain.site.com
bit.subdomain.site.com
etc

子子域可以是任何东西,但子域始终是相同的。

我需要将它们重定向到:

quarksubdomain.site.com
yysubdomain.site.com
bitsubdomain.site.com
etc

所以基本上,我只需要删除点。

使用这个问题的答案,我能够做到这一点: .htaccess 301 redirect one subdomain to another, for multiple TLDs

但这是分别执行每个子子域:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^quark.subdomain\.(.+)$ [NC]
RewriteRule ^ https://quarksubdomain.%1 [L,R=301]

RewriteCond %{HTTP_HOST} ^yy.subdomain\.(.+)$ [NC]
RewriteRule ^ https://yysubdomain.%1 [L,R=301]

但我有30个左右的这些子子域,所以我想要更高效的东西。

1)我是否可以使用通配符方法从子域的任何子子域中删除点?

2)我未能通过域名后面的地址的任何部分。我正在使用CakePHP,因此REQUEST_URI不起作用,因为它为我提供了Cake隐藏的地址信息。如果不使用REQUEST_URI,最好的方法是什么?

为初学者解释REGEX特殊字符的链接的奖励积分。我想纠正我的无知。

2 个答案:

答案 0 :(得分:0)

您不需要从条件中获取请求URI,您可以从重写规则中获取,并优化您从cond中保留的内容:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.subdomain\.site\.com$ [NC]
RewriteRule (.+) https://%1subdomain.site.com$1 [L,R=301]

奖励点......我不知道,https://regexone.com/可能吗?

答案 1 :(得分:0)

经过一些反复试验,我终于成功了以下内容:

RewriteCond %{HTTP_HOST} ^(.+)\.subdomain\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%subdomain.%2/$1 [QSA,L,R=301]

它正确获取任何前缀并将其附加到子域名,并且还正确传递.com /之后的位置。我也使它对域不敏感,因此它也可以在我的开发域中本地工作。

我无法给自己任何奖励积分,因为我仍然无法解释为什么必须通过这种方式传递特定的网址。