htaccess通过删除点从几个子域重定向(htaccess redirect from several subdomains by removing the dot)

编程入门 行业动态 更新时间:2024-10-26 05:33:34
htaccess通过删除点从几个子域重定向(htaccess redirect from several subdomains by removing the dot)

我有一组需要在.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将一个子域重定向到另一个子域,用于多个TLD

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

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)我无法通过domain.com/后面的地址的任何部分。 我使用CakePHP,所以REQUEST_URI不起作用,因为它给了我Cake的隐藏地址信息。 不使用REQUEST_URI的最佳方式是什么?

奖励分数,用于解释初学者的REGEX特殊字符的链接。 我想纠正我的无知。

I have a set of sub-subdomains that I need to redirect in .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.

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

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.

1) Is there a wildcard method I can use to just remove the dot from any sub-subdomain of subdomain?

2) I am failing to pass any part of the address that comes after the domain.com/ . I am using CakePHP, so REQUEST_URI does not work, because it gives me address information that Cake hides. What is the best way to do this without using REQUEST_URI?

Bonus points for a link that explains the REGEX special characters for beginners. I would like to correct my ignorance.

最满意答案

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

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

奖励点...我不知道, 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. 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.

更多推荐

本文发布于:2023-08-06 20:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455344.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:个子   重定向   htaccess   redirect   dot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!