nix, shell, perl, php, mysql and mac os x tips and tricks

Friday, January 20, 2012

mod_rewrite magic! Redirecting "behind the scenes"

I've posted about this before, but I have found another use for this trick. A client wanted to put dynamic JSP function on one of their sites, but the site in question was a simple apache vhost that was not running the tomcat application server. I didn't want to create a new instance of the application server and basically have to move the site just to add this little function they wanted...So, I built the JSP pages on another server (on a completely different domain) and used this mod_rewrite trick to make it appear to the user that they haven't left the site. Beautiful! You'd put something like this in your .htaccess file:
RewriteEngine On
RewriteBase    /

# first, make sure all http hosts have the www.
RewriteCond %{HTTP_HOST} !^www\.yoursite\.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

# check for any hits to the specific application page you want to forward to.
# the second rewritecond is very important, otherwise ALL hits will be forwarded
# add additional rewritecond's for each app page you want forwarded
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteCond %{REQUEST_URI} ^/dynamic_catalog [NC]
RewriteRule (.*) http://www.yourapplicationsite.com/%{REQUEST_URI} [L,P]

No comments:

Post a Comment