2

This below rewrite redirects localhost to http://www.example.com/?id=211&test=1 but I want that localhost on browser should not be changed but the page will come form the above link.

I am using this rewrite rule on my Apache conf:

 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^localhost$ [OR]
 RewriteRule ^/?$ http://www.example.com/?id=211&test=1 [L]

1 Answer 1

2

This below rewrite redirects localhost to http://www.example.com/?id=211&test=1 but I want that localhost on browser should not be changed but the page will come form the above link.

If you want to load a page from elsewhere without chaining the URL, mod_rewrite is the wrong tool for the job. Use mod_proxy instead. First enable it in Apache like this; example assumes you are on Ubuntu 12.04 but should work on most any Linux Apache install

sudo a2enmod proxy proxy_http

Then set this to enable a reverse proxy from your root path of / to http://www.example.com/?id=211&test=1:

<IfModule mod_proxy.c>

  # Proxy specific settings
  ProxyRequests Off
  ProxyPreserveHost On

  <Proxy *>
    AddDefaultCharset off
    Order deny,allow
    Allow from all
  </Proxy>

  ProxyPass / http://www.example.com/?id=211&test=1
  ProxyPassReverse / http://www.example.com/?id=211&test=1

</IfModule>

EDIT: Seems like mod_proxy and query strings for the destination do not mix; emphasis mine:

This directive allows remote servers to be mapped into the space of the local server; the local server does not act as a proxy in the conventional sense, but appears to be a mirror of the remote server. The local server is often called a reverse proxy or gateway. The path is the name of a local virtual path; url is a partial URL for the remote server and cannot include a query string.

So if there is anyway you could set another page—maybe on localhost—that would bounce it behind the scenes. Meaning this happens on localhost:

ProxyPass / bounce.php

And then the file, bounce.php could have this line in it:

<?php
  header('Location: http://www.example.com/?id=211&test=1');
?>

Which would allow mod_proxy to have a valid destination. And then the PHP file does the redirect? Hard to say, but the query string on your destination server is the issue.

Sign up to request clarification or add additional context in comments.

4 Comments

One issue i forgot to mention "on localhost I have apache running" and on [link](example.com) nginx is running. So I am now getting 404 Not Found error with above configuration on localhost
Considering dependency proxy for proxy_http: Module proxy already enabled Module proxy_http already enabled ERROR: Module enabled does not exist! But I am still getting 404 not Found page when hitting localhost
curl gives me this HTTP/1.1 404 Not Found Date: Fri, 20 Jun 2014 06:49:29 GMT Server: nginx Content-Type: text/html Content-Length: 162 Vary: Accept-Encoding
@JakeGould ProxyPass / (example.com) [Working] ProxyPass / (example.com/?id=211&test=1) [css/image/js] not loading

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.