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

Redirecting from www.example.com/blog to blog.example.com

· 52CE 我爱测

I am about to switch my blog onto a subdomain blog.example.com so I can have two different WordPress themes installed. Currently, my blog resides at example.com/blog and I was wondering if there were any special 301 redirects or …

问题原文

原帖链接 · jenna · CC BY-SA 3.0

I am about to switch my blog onto a subdomain blog.example.com so I can have two different WordPress themes installed. Currently, my blog resides at example.com/blog and I was wondering if there were any special 301 redirects or issues that may arise? Would the below 301 redirect format work?

RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^blog/$ http://blog.example.com/? [L,R=301]

or should it be

Redirect 301 /blog http://blog.example.com/

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

Since you mention WordPress then only the first (mod_rewrite) solution will work reliably. WordPress uses mod_rewrite for internal routing, so you should also use mod_rewrite for external redirects. Redirect is a mod_alias directive and executes later in the request, so you could end up with a confusing redirect.

However, the redirect posted only redirects the home page of the blog. Presumably you also want to redirect all the internal pages as well? In which case you will need something like the following in .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [L,R=301]

However, it might also depend on where the main domain and subdomain are pointing. Do they point to the same place on the file system? A subdirectory? Somewhere entirely different?

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

资料来源与适用范围

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

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

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