Forum Moderators: phranque

Message Too Old, No Replies

URL re-write query.

         

spike2000

1:25 pm on Nov 28, 2008 (gmt 0)

10+ Year Member



I have a virtual host entry in my httpd.conf (www.sitea) which currently re-directs to a Tomcat instance via Proxypass configuration. Whilst I still wan't default traffic to go to the Tomcat Instance, I want to re-direct certian URL's to other sites;

For example, www.sitea.com/siteb or site.com/siteb shoud re-direct to www.siteb.com

This is my vhost entry

<VirtualHost 211.111.111.11:80>
ServerName sitea.com
ServerAlias www.sitea.com
DocumentRoot /home/sitea/public_html
ProxyPass / [servera.hosting.com:8080...]
ProxyPassReverse / [servera.hosting.com:8080...]
</VirtualHost>

I have set RewriteEngine on in my httpd.conf and have tried changing the vhost configuration as follows;

<VirtualHost 211.111.111.11:80>
ServerName sitea.com
ServerAlias www.sitea.com
DocumentRoot /home/sitea/public_html
RewriteRule ^site.com/siteb$ www.siteb.com
ProxyPass / [servera.hosting.com:8080...]
ProxyPassReverse / [servera.hosting.com:8080...]
</VirtualHost>

However this isn't working and I just get a page not found error. Can anyone please advise if and where I'm going wrong? Thank you.

g1smd

2:03 pm on Nov 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteRule only sees paths within a site, not domain names.

You need a RewriteCond to test the HTTP_HOST if you need to identify the domain.

spike2000

1:08 pm on Dec 3, 2008 (gmt 0)

10+ Year Member



<VirtualHost 211.111.111.11:80>
ServerName sitea.com
Hi

Thanks for your response. I tried modifying the URL re-write as follows;

RewriteCond %{HTTP_HOST} ^site.com/siteb$\.
RewriteRule ^/(.*) [siteb.com...] [P]

But still not working. Can you advise what the problem is?

spike2000

1:09 pm on Dec 3, 2008 (gmt 0)

10+ Year Member



Apologies, the section in top of my post was done in error.

<VirtualHost 211.111.111.11:80>
ServerName sitea.com

g1smd

8:22 pm on Dec 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** RewriteCond %{HTTP_HOST} ^site.com/siteb$\. ***

HTTP_HOST sees only the domain name, not any server folder/file path after the domain name.

jdMorgan

1:13 am on Dec 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to permanently redirect [sitea.com...] and [sitea.com...] to [siteb.com...] then that could be done with:

RewriteCond %{HTTP_HOST} ^(www\.)?sitea\.com
RewriteRule ^/siteb/(.*)$ http://www.siteb.com/$1 [R=301,L]

Also, since this code is within the vHost container for SiteA, the RewriteCond may not be required.

Be sure to redirect to the canonical hostname of SiteB -- Pick either www.siteb.com or siteb.com, and be consistent to avoid duplicate-content problems. If you are not the owner of SiteB, then ask them what their canonical domain preference is.

Jim

spike2000

7:24 am on Dec 4, 2008 (gmt 0)

10+ Year Member



Hi

Thanks JD, that worked a treat. Much appreciated. Out of interest will the request preserve the orginal headers of the URL request?

Regards
Michael

P.S. Can you suggest a good tutorial \ book on URL re-writing?

jdMorgan

11:56 am on Dec 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, an external redirect terminates the current HTTP transaction with the client, and tells it to re-request the resource it originally asked for using the new URL given in the 301 redirect response. So, the original request is terminated, and the client must initiate a new one.

You might look into reverse proxying the request to the other server, or internally rewriting it if the two domains share filespace on the same server. Mod_rewrite can do external redirects, proxy throughputs, and internal rewrites, depending on the syntax you specify.

Be aware that a request proxied to another machine will appear to that second machine as originating from the first server, not from the client. This makes the server access logs on the back-end server kind of useless; To overcome this, the front-end server can be configured to send the originating IP address with an HTTP X-Forwarded-For header, and the back-end server can be configured to use that header instead of Remote_Addr for logging purposes.

There are several useful resources cited in our Forum Charter, the Apache mod_rewrite documentation and the Apache URL Rewriting Guide among them. These are worth a thorough read. Also, we have several tutorials in the Apache section of the WebmasterWorld Library. See the links at the top of each page here.

As for printed books, there are a few good ones available through Amazon, B&N, etc. on-line.

Jim

spike2000

12:20 pm on Dec 4, 2008 (gmt 0)

10+ Year Member



Thanks JD