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

How to redirect 301 within subdomain?

· 52CE 我爱测

I have 2 campaigns at competition.example.com/option_1 competition.example.com/option_2 option_1 and optino_2 are in separate folders which are in root folder. Each file and root has its own .htaccess. Content of: .htaccess of op…

问题原文

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

I have 2 campaigns at

  • competition.example.com/option_1
  • competition.example.com/option_2

option_1 and optino_2 are in separate folders which are in root folder. Each file and root has its own .htaccess.

Content of:
.htaccess of option_1
.htaccess of root

How and where (which .htaccess) can I 301 redirect from competition.example.com/option_1 to competition.example.com/option_2?

已采纳解答

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

In the .htaccess file where competition.example.com points to on the filesystem, you can use a mod_alias Redirect:

Redirect 301 /option_1 /option_2

This assumes you don't have any existing mod_rewrite (ie. RewriteRule) redirects that might conflict.

Note that .htaccess files inherit along the filesystem path (regardless of whether you are using subdomains or not). So if the competition subdomain actually points to a /competition subdirectory off the main domain's document root, then the .htaccess file in the parent directory (the main domain's document root) will be processed second.

However, by default, mod_rewrite directives are not inherited (mod_rewrite is different to other modules in this respect). So directives in the child (subdirectory) .htaccess file will completely override the parent.


UPDATE: Since you are using mod_rewrite throughout your root .htaccess file you should change the above (mod_alias) Redirect to use mod_rewrite as well. mod_rewrite will execute before mod_alias, regardless of the order of directives in .htaccess, so it's quite possible there is a conflict.

At the very top of your root .htaccess file (which I assume is the root of subdomain), try the following:

RewriteRule ^option_1(.*)$ /option_2$1 [R=302,L]

This assumes you don't have files that start "option_1".

Change the 302 (temporary) redirect to 301 (permanent) redirect when you are sure it's working OK. 301 redirects are cached by the browser, so can make testing problematic. For this reason you need to make sure your browser cache is emptied before testing.

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

资料来源与适用范围

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

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

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