大网数据广告活动
网站运营与故障问答

How do I redirect a subdomain with a wildcard?

· 52CE 我爱测

I've an old subdomain that was being used as a whitelabel. I'd like to redirect the subdomain to a page on my main domain. I was thinking the best way would be a htaccess redirect. RewriteCond %{HTTP_HOST} ^mysubdomain\.example\.…

问题原文

原帖链接 · Dtheme · CC BY-SA 4.0

I've an old subdomain that was being used as a whitelabel.

I'd like to redirect the subdomain to a page on my main domain.

I was thinking the best way would be a htaccess redirect.

RewriteCond %{HTTP_HOST} ^mysubdomain\.example\.com$ [NC]
 RewriteRule ^(.*) https://www.example.com/newpage.html/$1 [L,R]

I've a feeling the above rewrite is not correct.

I've also got another redirect in my htaccess for the main domain so I'm not sure where to put this in?

Finally, is there another way of doing this?

已采纳解答

原帖链接 · closetnoc · CC BY-SA 4.0

You are close.

RewriteCond %{HTTP_HOST} ^sub-domain\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/newpage.html [R=301,L]

--or--

RewriteCond %{HTTP_HOST} ^sub-domain\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/folder/ [R=301,L]

I included the second example in case you want it.

I also use R=301,L. R,L is nearly the same thing defaulting to a 302 redirect which is temporary whereas a 301 is permanent. A 301 is what you want. I like to be explicit when using R.

Notice I use both the ^ and $ to properly define the beginning and end of any regex (regular expression). I have learned to avoid misinterpretation where possible. Again, I like to be explicit.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2019-10-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。