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

Apache: redirecting an entire directory to the root of another site

· 52CE 我爱测

I want to redirect everything from a certain subdirectory to the root of another site, i.e. without the subdirectories: example.com/jobs should be redirected to jobs.example.net everything path behind /jobs should be omitted Thus…

问题原文

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

I want to redirect everything from a certain subdirectory to the root of another site, i.e. without the subdirectories:

  • example.com/jobs should be redirected to jobs.example.net
  • everything path behind /jobs should be omitted
    • Thus, example.com/jobs/foo should also be redirected to jobs.example.net

This last part is the most difficult as with Redirect 301 /jobs jobs.example.net the path /foo is not omitted. Thus, example.com/jobs/foo is redirected to jobs.example.net/foo which throws a 404 Not Found error, as this page doesn't exist.

How can I achieve that the path is omitted in a redirect?

已采纳解答

原帖链接 · Esa Jokinen · CC BY-SA 4.0

This is not possible with mod_alias Redirect, but with RedirectMatch. If you use a regular expression, but leave the $1 substitution for (.*) out of the URL, it will redirect to that exact URL.

RedirectMatch 301 "^/jobs(.*)" "http://jobs.example.net/"

Then, you will probably find many examples on how to do this with mod_rewrite, but you should avoid using it when there is a mod_alias alternative, as above. For completeness, here's a mod_rewrite alternative:

RewriteEngine on
RewriteRule "^/jobs" "http://jobs.example.net/" [R=301,L]

Both will redirect everything starting with /job, including /joblessness; not just the URLs /job and /job/*. If you wish to avoid that, you could put the rules inside a <Directory /path/to/jobs> section.

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

资料来源与适用范围

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

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

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