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

11,203

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 www.myurl.com/some/test/url 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 http://www.blahblah.com/something), 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} ^DomainA.com
RewriteRule ^(.*) http://DomainB.com/$1 [P]

It works for me.

Source: http://www.inmotionhosting.com/support/website/htaccess/redirect-without-changing-url

Share:
11,203
Satchel
Author by

Satchel

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

Updated on June 04, 2022

Comments

  • Satchel
    Satchel almost 2 years

    I have a url that is www.blahblah.com/something

    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 www.myurl.com so that the content displayed is all www.blahblah.com/something, but the address bar still reads www.myurl.com

  • Satchel
    Satchel about 12 years
    Hmm...well the myurl.com 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 12 years
    is this a redirect or a rewrite...in other words, in the navigation wlil it show myurl.com, but the page served will ceom from www.blahblah.com/something/something?
  • Satchel
    Satchel about 12 years
    I noticed you just did a rewrite with the URI, but not with the directories for blahblah.com/something else....
  • cegfault
    cegfault about 12 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 12 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 almost 9 years
    Could you please elaborate more your answer adding a little more description about the solution you provide?