Forum Moderators: phranque

Message Too Old, No Replies

Dynamically Re-direct URL Strings.

         

dburnsfca

7:07 pm on Dec 4, 2006 (gmt 0)

10+ Year Member



Hello All,

I want to know if Apache can dynamically create a URL string based on a URL entered in a web browser.

I've researched mod_rewrite and obviously it's one of the more advanced Apache features.

So here goes...

A user will enter the address below. The unique 11 character ending is different for each page.

http://www.example.org/campcode.html?camp=NEHA0618LDC

We want the server to take the last 11 characters and place them in a new URL string and pass the URL to the server.

http://www.example.org/campcode.html?camp=https://teamnet.fca.org/camp/info/NEHA0618LDC?OpenDocument

Is this possible using mod_rewrite?
Anyone have any other ideas? Code?

I've never used mod_rewrite. It looks like a beast!

We're trying to make the URL smaller and more manageable and letting the server do the work.

Any help is greatly appreciated!
Thanks!
-Danny

[edited by: jdMorgan at 10:59 pm (utc) on Dec. 4, 2006]
[edit reason] Examplified. No URLs, please. See TOS. [/edit]

jdMorgan

3:23 pm on Dec 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is actually a relatively-simple application of mod_rewite at the URL-rewriting level.

The concepts you need are:

  • Pattern-matching using regular expressions
  • Capturing query strings using RewriteCond and the %{QUERY_STRING} environment variable.
  • Back-references (copying all or part of a matched pattern to the new URL)

    In .htaccess, the code would look something like this (adjust to suit).


    RewriteCond %{QUERY_STRING} ^camp=([A-Z0-9]+)$
    RewriteRule /campcode\.html$ /campcode.html?camp=https://teamnet.example.org/camp/info/%1\%3fOpenDocument [NE,L]

    Note that I changed the second "?" in your long URL to an escaped hex-encoded character and added the [NE] flag -- Using two question marks in a query string is a violation of the HTTP/1.1 specification, and I cannot recommend it. If this does not work, you may need to use another rewrite on the HTTPS server to modify a URL that's acceptable for passing through the HTTP server (for example, using an ampersand instead of the second "?") into the correct one for the HTTPS server.

    For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

    Jim

  •