How do I do a .htaccess rewrite that masks the forwarded URL?


Solution 1

No, this is not possible with foreign urls.

You can, however, do this locally. For example, look at this htaccess file:

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteRule ^some/test/url$ index.php?some=test&or=url [L]

In this scenario, if you visit it will show as such on the browser, but your server will actually be running index.php in your document root with the parameters some=test&or=url.

This is only possible for scripts running on your server. You cannot do this on another server/domain. If you try this (eg, by changing index.php?some=test&or=url in the example above to, then apache will just redirect the browser to that url.

htaccess (Apache) makes the connection to the user, and the user is expecting a response from YOUR server. If you try to load content from another server, Apache would have to make that connection, load the resulting HTML or whatever, and pass it back to you. But this gets messy, especially when you get into cookies, SSL, javascript, etc.

My question is: why do you actually need this? I'm not sure I understand why it is a problem if the user's url changes. If it's a service you have no control over, why is it so bad to just send them to it?

You might want to research more about cache servers, or using PHP to to make the http call to the server you want and "pass through" the content, assuming you know beyond a doubt there will be no issues with cookies or SSL or whatever. But again, why not just send them to the proper URL?

Solution 2

Try this:

RewriteCond %{HTTP_HOST} ^
RewriteRule ^(.*)$1 [P]

It works for me.


Author by


Former electrical engineer but learning ruby from scratch thanks to stack overflow. Still so much to learn.

Updated on June 04, 2022


  • Satchel
    Satchel 8 months

    I have a url that is

    That is a remote service, I don't have anything to do with it.

    How can I use .htaccess on my own server and rewrite from so that the content displayed is all, but the address bar still reads

  • Satchel
    Satchel about 11 years
    Hmm...well the is my server. The external service is not as wellformed and we want to build up presence on the Internet under our domain....I believe I've seen it done before...but maybe not....
  • Satchel
    Satchel about 11 years
    is this a redirect or a other words, in the navigation wlil it show, but the page served will ceom from
  • Satchel
    Satchel about 11 years
    I noticed you just did a rewrite with the URI, but not with the directories for else....
  • cegfault
    cegfault about 11 years
    I've done lots of things where we pull in a remote service, but it's always in PHP, through some type of REST-ful call or API. This is not possible to be done on the Apache/htaccess level, but will be a more complicated solution using a fair amount of programming.
  • cegfault
    cegfault about 11 years
    Without knowing specifically what the external service is, what exactly you are trying to do, and how much control/access you have to the external service (specifically on the back-end), I'm afraid I can't be of more help :/
  • abarisone
    abarisone over 7 years
    Could you please elaborate more your answer adding a little more description about the solution you provide?