问题原文
原帖链接 · 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/jobsshould be redirected tojobs.example.net- everything path behind
/jobsshould be omitted- Thus,
example.com/jobs/fooshould also be redirected tojobs.example.net
- Thus,
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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
