301 Redirect using OCPsoft Rewrite

Splash Forums Rewrite Users 301 Redirect using OCPsoft Rewrite

Tagged: 

This topic contains 5 replies, has 2 voices, and was last updated by  Christian Kaltepoth 1 year, 6 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #26825

    eframsergio
    Participant

    Hi ya’ll!

    Is it acceptable to use OCPsoft Rewrite code for redirecting from old website to new website instead of using Apache (using standalone Tomcat) .htaccess rewrite rules?

    So I basically want to 301 redirect (as recommended in webmasters tools docs) from my old domain name to new one.

    So anyone typing http://www.oldwebsite.com, should be 301 redirected to http://www.newwebsite.com.

    If so, are there links to an example syntax to do this?

    Thanks in advance!
    sb

    #26827

    To be honest, I think it is much easier to do this with HTTPD. Rewrite is basically specialized in rewriting the path part of the URLs. Doing hostname rewrites is also possible, but I guess using HTTPD is much simpler.

    #26839

    eframsergio
    Participant

    Hi Christian!

    Since I’m already using Rewrite to pretty up some of my URLs, I thought it would be easier just to add a new rule to achieve this as opposed to installing and configuring Apache to sit in front of Tomcat…

    What is the complicated aspect of doing it with rewrite or drawbacks you see? I would much rather do it with rewrite if I can.

    By the way, I seem to be trying to solve the same issue described here: http://nematodes.org/martin/2010/02/04/301-permanent-redirect-with-tomcat-howto/

    Martin, the author of the article provides a solution already with the proposed library and xml configuration file. It’s great, but it seems to me that Rewrite already provides this mechanism. What do you think?

    Thanks in advance!
    sb

    • This reply was modified 1 year, 6 months ago by  eframsergio.
    #26844

    It is not that complicated. My personal opinion on that is just that the app server shouldn’t bother which host name is used. IMHO that is up to the reverse proxy which is usually a httpd. So if you already have an httpd as a reverse proxy, it is usually easier to configure something like that there.

    However, if you want to implement this requirement with Rewrite, you could try something like this:

    .addRule()
    .when( Direction.isInbound().and( 
        Domain.matches( "www.old-domain.com" ).and( 
        Path.matches( "{path}" ) ) ) )
    .perform( Redirect.permanent( "http://www.new-domain.com/{path}" ) )
    .where( "path" ).matches( ".*" )
    

    I didn’t test it but it should be a good start.

    #26848

    eframsergio
    Participant

    Thank you! I appreciate your help and the sample rule.

    I had to place it on top of the other rules and just remove the forward dash in the redirect (i.e. “http://www.new-domain.com/{path}” to “http://www.new-domain.com{path}”).

    Just one last question, is “{path}” a reserved keyword or variable? It seemed like you placed it there as a placeholder so that I could fill in the actual path, but since I wanted everything in the old path to forward to the new domain name, it didn’t make much sense when I first looked at it, but the fact that it works perfectly tells me this is a reserved keyword or automatic variable.

    Best regards!
    sb

    #26852

    In my sample rule {path} is a parameter. That’s one of the key features of Rewrite. You can capture values in the rule condition and use it in the operation part.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

Comments are closed.