简体   繁体   English

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

[英]htaccess redirect from several subdomains by removing the dot

I have a set of sub-subdomains that I need to redirect in .htaccess. 我需要在.htaccess中重定向一组子子网域。 I'm not good at REGEX, and have failed to understand other posts I've read well enough to apply it to this situation. 我不太擅长REGEX,也无法理解我读得很好的其他文章,无法将其应用于这种情况。

My sites are laid out like this: 我的网站布局如下:

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

Where the sub-subdomain can be anything, but the subdomain is always the same. 子子域可以是任何东西,但子域始终相同。

I need to redirect them to: 我需要将它们重定向到:

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

So basically, I just need to remove the dot. 所以基本上,我只需要删除点。

I was able to get this far, using the answer from this question: .htaccess 301 redirect one subdomain to another, for multiple TLDs 通过使用以下问题的答案,我能够做到这一点: .htaccess 301将一个子域重定向到另一个,用于多个TLD

But this is doing each sub-subdomain individually: 但这是分别处理每个子子域:

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]

But I have 30 or so of these sub-subdomains, so I'd like something more efficient. 但是我有30个左右的子子网域,所以我想更高效一些。

1) Is there a wildcard method I can use to just remove the dot from any sub-subdomain of subdomain? 1)是否可以使用通配符方法从子域的任何子子域中删除点?

2) I am failing to pass any part of the address that comes after the domain.com/ . 2)我无法传递domain.com/之后的地址的任何部分。 I am using CakePHP, so REQUEST_URI does not work, because it gives me address information that Cake hides. 我正在使用CakePHP,所以REQUEST_URI不起作用,因为它为我提供了Cake隐藏的地址信息。 What is the best way to do this without using REQUEST_URI? 不使用REQUEST_URI的最佳方法是什么?

Bonus points for a link that explains the REGEX special characters for beginners. 奖励积分的链接为初学者解释REGEX特殊字符。 I would like to correct my ignorance. 我想纠正我的无知。

You don't need to get the Request URI from the condition, you can get if from the rewrite rule, and optimise what you keep from the cond: 您不需要从条件中获取请求URI,可以从重写规则中获取if,并优化从条件保留的内容:

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

Bonus point... I don't know, https://regexone.com/ maybe? 奖励点...我不知道,也许是https://regexone.com/

After some trial and error, I was finally successful with the following: 经过反复试验,我终于成功实现了以下目标:

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

It correctly gets any prefix and appends it to the subdomain name, and also passes the locations following the .com/ correctly. 它可以正确获取任何前缀并将其附加到子域名,还可以正确传递.com /之后的位置。 I also made it domain insensitive, so it works natively on my development domain also. 我还使它对域不敏感,因此它也可以在我的开发域上本地运行。

I can't give myself any bonus points though, because I still can't explain why it has to be exactly this way to pass the specific page address. 但是我无法给自己任何奖励积分,因为我仍然无法解释为什么必须使用这种方式才能传递特定的页面地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM