Is there any way of doing a mod_rewrite from one domain to another, but keeping the browser URL the same throughout? This is what I want:

User enters something like bacon.com and ends up on example.com/bacon, but the browser URL always stays as bacon.com?

I basically want to do a mod_rewrite whilst masking the URL. Sorry for the unimaginative example domains, but it's still only 10am... I've checked lots of other answers, but they always usually involve mod_rewrites on the same domain. They don't deal with the fact that bacon.com is already a ServerAlias of example.com.

  • 1
    You can serve the page example.com/bacon in various ways, such as proxying it, but there's a bunch of other issues: which domain gets to get cookies? What is the base URL considered to be for image and CSS requests?
    – pjc50
    May 10, 2013 at 9:16

1 Answer 1


I can give you three ways of doing this.

1 Using virtual hosts

Given that bacon.com is a ServerAlias for example.com, i.e. they're both on the same server, you can do this without using any mod_rewrite at all. Think of it this way: mod_rewrite is at its most basic a way to map URLs to a file system. So what I would do is to simply set up a separate VirtualHost that uses the correct directory as its base.

Here's a brief example:

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName example.com
  DocumentRoot /www/files

  [... all other config you have for example.com]

<VirtualHost *:80>
  ServerName bacon.com
  DocumentRoot /www/files/bacon

  # To have CSS directly under /www/files/css instead of /www/files/bacon/css
  Alias /css /www/files/css

  [... all other config you have for bacon.com]

As you see, this isn't a rewrite; instead you're setting bacon.com up to use the base directory of example.com/bacon as its own DocumentRoot.

2 Using mod_rewrite as a proxy

If you do want to use mod_rewrite instead, you can have it use the P flag which will make mod_rewrite act as a proxy. Here's an example:

RewriteRule /(.*) http://example.com/bacon/$1 [P]

Edit in answer to the comment:

Since you want the http://bacon.com/css to be http://example.com/css instead of http://example.com/bacon/css, make a separate rule that goes first and catches that specific URL:

RewriteRule /css/(.*) http://example.com/css/$1 [P]
RewriteRule /(.*) http://example.com/bacon/$1 [P]

In order to do this, you need to have mod_proxy loaded and enabled. Do however note that this will reduce performance compared to using mod_proxy directly, as it doesn't handle persistent connections or connection pooling. So if you can't do what you want with VirtualHosts, I'd suggest the third method:

3 Using mod_proxy directly

<VirtualHost *:80>
  ServerName bacon.com

  ProxyPass http://example.com/bacon
  ProxyPassReverse  http://example.com/bacon

For more information about that, see the mod_proxy documentation

  • 1
    +1 for the [P] flag (which works), but now I'm getting no styles. How can I get the mod_rewrite to ignore URLs for css (so bacon.com/css/ should be redirected to example.com/css)
    – hohner
    May 10, 2013 at 10:26
  • I'm adding that to the answer, give me a sec...
    – Jenny D
    May 10, 2013 at 10:36
  • 1
    Thanks for the [P] flag. Exactly what I was looking for: simple, and just working. :)
    – Tom
    Jun 26, 2018 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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