2

I'm having a problem running Apache 2.2.20 on a Linux Mint distro.

I've got the following rule in my .htaccess file:

RewriteEngine on
RewriteRule old1/old2 http://localhost/new1 [P]

When I browse to http://localhost/old1/old2 I just get a redirect to localhost/new1. That is, the new URL shows in the address bar. I want the "old1/old2" URL to show (with the page content from /new1). What am I doing wrong?

I've installed mod_proxy and mod_proxy_http (i.e. a2enmod proxy, a2enmod proxy_http).

masegaloeh
  • 18,236
  • 10
  • 57
  • 106
Monkeybrain
  • 121
  • 1
  • 2

3 Answers3

2

If both resources are on the same web-server, simply do an internal rewrite e.g.

RewriteEngine on
RewriteRule old1/old2 /new1 [L]
arober11
  • 426
  • 3
  • 7
  • 1
    This would mean /new1 was visible in the URL wouldn't it? I want to fetch data from /new1 but display the requested URL old1/old2 ? – Monkeybrain Jan 18 '13 at 14:34
  • No, the old URL will remain visible, as an INTERNAL not a CLIENT rewrite. – arober11 Jan 18 '13 at 14:44
  • Just tried that and unfortunately I'm not seeing that happening - the browser gets the /new1 page but also the /new1 URL. – Monkeybrain Jan 18 '13 at 14:54
  • The above syntax will perform an internal rewrite (does not alter the URL in the browser), so try another browser flavour. If the rule still doesn't work an earlier rule or syntax error is likely to be at fault, in your config. If your other browser works as expected, clear the browser cache on the other browser, and re-test. See: [Rewriting From Old to New (external)](http://httpd.apache.org/docs/2.4/rewrite/remapping.html#old-to-new-extern) – arober11 Jan 18 '13 at 15:39
  • That's a useful link, thanks. It does seem to be saying the opposite though - From Old To New (Internal) uses the [PT] flag, which doesn't change the URL in the browser, while From Old To New (External) does not specify the proxy flag, and therefore the URL does change in the browser. Therefore I need to use the proxy flag [P] ? – Monkeybrain Jan 18 '13 at 15:57
  • OK here's a weird thing - putting a trailing slash on the destination URL seems to fix my problem - the proxy works OK, and old1/old2 is shown in the browser. I'm guessing that without one, the DirectorySlash directive kicks in, and rewrites the URL from new1 to new1/. Thanks for the helpful discussion. – Monkeybrain Jan 18 '13 at 16:46
  • @arober11 This does not perform an internal rewrite, the [PT] flag is needed for an internal rewrite, you linked the external rewrite documentation.. – Michael Aug 25 '14 at 02:17
1

I think you also need a ProxyPassReverse statement. Here's an example from http://httpd.apache.org/docs/2.4/rewrite/proxy.html

RewriteEngine  on
RewriteBase    /products/
RewriteRule    ^widget/(.*)$  http://product.example.com/widget/$1  [P]
ProxyPassReverse /products/widget/ http://product.example.com/widget/
dough
  • 316
  • 1
  • 3
-2

RewriteEngine on RewriteRule ^old1/old2*$ http://localhost/new1 [P] RewriteRule ^old1/old2/*$ http://localhost/new1 [P]

If doesn't work try change "localhost" to "localhost:80".