tags:

views:

241

answers:

3

Hi - I've had a look through the many mod_rewrite examples, but I can't find out how to rewrite the URL so that a query to one domain gets redirected to the new domain, and has a specific URL parameter added. I need to achieve the following:

a) change the server name
b) change the path to incude the application name
c) append URL parameter

eg:

example.com/index.html  -->  new_example.com/app/index.html?user=XX
example.com/page1.html  -->  new_example.com/app/page1.html?user=XX
example.com/page2.html  -->  new_example.com/app/page2.html?user=XX
example.com/page3.html  -->  new_example.com/app/page3.html?user=XX

Thanks in advance, Kevin.

A: 

You can absolutely use mod_rewrite to redirect to a different domain; you just can't use it to rewrite to a different domain (i.e., keep the URL in the browser yet serve content from another site).

Take a look at this article on askapache.com for some examples of various forms of redirection.

In this case, you probably want something like:

RewriteRule .* http://www.new_example.com/app/$0?user=XX [R=301,L]
VoteyDisciple
Use `$0` instead of `$1`.
Gumbo
I guess the crux of the issue is that I want to achieve a rewrite to a different domain, not a redirect. I applied the rule you provided and it works to the extent that I am redirected. Is it possible to have a rewrite solution, if not with mod_rewrite, then with some other technique ?
Kevin
A: 

I may not have the exact same setup as you, but this currently works for me:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName main:80
    DocumentRoot ...
</VirtualHost>

<VirtualHost *:80>
    ServerName alt:80
    RewriteEngine On
    RewriteCond %{HTTP_HOST}  ^alt
    RewriteRule ^/(.*) http://alt:8080/$1 [P]
</VirtualHost>

I then have a separate Apache server instance, with its own conf file that directs it to listen on 8080. The [P] is for proxy, I think I got it in one of the advanced examples.

If this doesn't happen to work...

  1. What version of Apache are you using?
  2. Is the new domain hosted on the same computer, or by the same Apache instance, or separately?
Kev
BTW you'll also need to load mod_proxy for this...
Kev
A: 

It turns out you can use mod_rewrite to rewrite to a different domain, using the [P] redirect flag. As I understand it, this is equivalent to mod_proxy.

What I needed was this:

example.com/index.html  -->  new_example.com/app/index.html?user=XX
example.com/page1.html  -->  new_example.com/app/page1.html?user=XX

and the rewrite rule to achieve this is:

RewriteRule .* http://new_example/app$0?user=XX [P]

Thanks for the input.

Kevin