Forum Moderators: phranque

Message Too Old, No Replies

redirect and manipulate URL help

         

Jayhovah

9:06 pm on Sep 10, 2009 (gmt 0)

10+ Year Member



Experts,

We have an internal apache server which just redirects some virtual hosts.

Now I am told that I will be getting requests of the nature:

http://server1/amy/bob/jimmy/1234567890/connie

where the bold numbers represent a unix timestamp..

I need to redirect this to:

http://server2/bob/amy/connie/MMDDYY/jimmy

Where the MMDDYY is the unix time stamp converted to a standard date format. Also notice other portions of the URL are rearranged.

I have no idea where to start... can you guys give me a general idea what I need to use to accomplish this?

Please... point me in the right direction!

Thanks,
Jay

[edited by: jdMorgan at 9:40 pm (utc) on Sep. 10, 2009]
[edit reason] de-linked [/edit]

jdMorgan

9:39 pm on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The re-arrangement isn't generally a problem, you could do that with mod_rewrite. But doing time-format conversion is well beyond the standard Apache modules' capabilities.

So, if you really want an external redirect (as opposed to an internal rewrite), I would suggest using mod_rewrite to internally rewrite all requests matching your URL "template" to a small PERL script which can do the time-format conversion and then send a redirect response to the client. This script would then execute in the content-handling phase of the API (mentioned only to contrast with the following).

If you do need a purely-internal-rewrite solution and have server config file access, look into using mod_rewrite's RewriteMap feature to call a PERL script in the URL-to-filename phase of the API, returning a converted-format internal path.

I'm being specific about internal rewrites and external redirects. If the difference is not crystal-clear, do not proceed until it is. A search of WebmasterWorld on "difference between rewrite redirect" may prove useful.

Jim

[edited by: jdMorgan at 4:26 pm (utc) on Sep. 11, 2009]

Jayhovah

3:13 pm on Sep 11, 2009 (gmt 0)

10+ Year Member



I definitely want to do an external redirect, as some of the requests may be directed to an SSL server.

Using a perl script to generate the redirect URL sounds ideal. Can you point me in the right direction? A link or search keywords like you provided before would be awesome.

Thanks so much for your help, it has already been very valuable.

wilderness

4:01 pm on Sep 11, 2009 (gmt 0)

Jayhovah

4:27 pm on Sep 11, 2009 (gmt 0)

10+ Year Member



Thanks for the speedy reply, wilderness.. Not quite what I was looking for, but I don't think I was very clear when I asked..
What I need to find out is, How do I pass a URL request to a perl script and then redirect the client to the URL returned by the perl script?

wilderness

4:32 pm on Sep 11, 2009 (gmt 0)

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



url+perl [google.com]

Jayhovah

5:07 pm on Sep 11, 2009 (gmt 0)

10+ Year Member



Hmm.. I am lost. =(

Perhaps I am thinking about this incorrectly?
Can I pass a URL to a perl script as a string and have the script return a string containing the URL I wish to redirect the client to? Or am I going the wrong direction here?

jdMorgan

5:14 pm on Sep 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We try to focus on very-specific questions here, and assume that you've done your research. Please see our Apache Forum Charter for more information about how to get the most from this forum.

[use] mod_rewrite to internally rewrite all requests matching your URL "template" to a small PERL script which can do the time-format conversion and [this script can] then send a redirect response to the client.

In this case (generating an external redirect as opposed to an internal rewrite), you could also use PHP if you are more comfortable with that scripting language. Both PERL and PHP have the functions needed to do time-format-conversion and to output the server status response (301-Moved Permanently) code and Location headers.

Jim

Jayhovah

6:03 pm on Sep 11, 2009 (gmt 0)

10+ Year Member



Ah, I think I am following you now. Thanks for the assistance.

Jayhovah

6:52 pm on Sep 14, 2009 (gmt 0)

10+ Year Member



Just an update for anyone who comes across this thread in a search, looking for some basic concepts as I was.

The solution for me was to setup a virtual host which executed a perl script regardless of the path in the request to that host.

Your httpd.conf would have a section like:

<VirtualHost *:80>
ServerName yourhost.domain.com
ScriptAliasMatch ^/.* /path/to/script.pl
</VirtualHost>

Which will take any http:80 request to yourhost.domain.com starting with a '/' and execute script.pl

From there, the you can access all the environment variables inside the perl script. The following script will just print all the variables.

#!/usr/bin/perl
print "Content-type: text/html\n\n";
foreach $key (keys %ENV) {
print "$key --> $ENV{$key}<br>";
}

From there, the responsibility is on the script to do whatever.
Thanks to everyone on here who chimed in and helped!

jdMorgan

8:20 pm on Sep 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could shorten your ScriptAliasMatch by eliminating the (unnecessary) ".*" on the end of the pattern. Since the pattern is not end-anchored, and ".*" matches "anything or nothing," the ".*" doesn't really do anything.

And having made that simplification, it then becomes obvious that since you want to match anything starting with a slash -- that is, any URL-path, the directive

ScriptAlias / /path/to/script.pl 

would be entirely equivalent (and simpler/faster to process).

If you're on Apache 2.x, see also the "AcceptPathInfo" directive, which might come in handy for passing the URL-path into your script for easy retrieval as PATH_INFO

Jim