-1

I want to redirect all domains from a specific directory to a new subdomain (invisibly).

e.g. example.com/literal/some/path/to/file.html -> sub.example.com/literal/some/path/to/file.html AND

www.example.com/literal/some/path/to/file.html -> www.sub.example.com/literal/some/path/to/file.html

Where "literal" should be constant, and anything that comes after it is arbitrary.

I've tried things like

RewriteEngine on RewriteCond %{HTTP_HOST} ^example\.com/literal RewriteRule ^(.*)$ http://sub.example.com/$1 [L]

But I can't get all the options right. Any help appreciated.

Sixhobbits
  • 101
  • 2

1 Answers1

2

The HTTP redirect is a visible method that causes the browser to go somewhere else, so it by definition rules your invisibly out. Depending on which one you prefer and is the new subdomain on the same server you have three alternative options.

  • As simple redirects as this one can be done with mod_alias Redirect. This will change the location on address bar.

  • If the content is on the same server and you want to invisibly rewrite the URL, the mod_rewrite's RewriteCond and RewriteRule are suitable. Just use local path instead of http:// in your RewriteRule's substitution.

  • If you need content from sub.example.com on an external server but don't want the location to change, you need to set up a reverse proxy with mod_proxy.

Once you have decided, your use case is a text book case: you can find a suitable example from any of the linked documentations.

Esa Jokinen
  • 43,252
  • 2
  • 75
  • 122