1

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?

2

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.

3
  • Thanks for your answer, I already tried using RedirectMatch but I inserted an $1 which had the same result as my redirect example in my original post. Regarding your remark, is this the good syntax to put it in a directory? <Directory /path/to/jobs> RedirectMatch 301 "^/(.*)" "http://jobs.example.net" </Directory>
    – wouter205
    Apr 14 '20 at 14:40
  • Looks ok to me. Apr 14 '20 at 14:58
  • I had to use <location> instead of <directory> as the path is not a real directory path but only a URL. The rest remains the same. Thanks for your good advise!
    – wouter205
    Apr 15 '20 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.