donglaoping9702 2017-05-11 23:50
浏览 43
已采纳

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

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.

  • 写回答

2条回答 默认 最新

  • douqiandai4327 2017-05-13 00:07
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮