Forum Moderators: phranque

Message Too Old, No Replies

RedirectMatch query

         

rjlfc

1:27 pm on Mar 13, 2008 (gmt 0)

10+ Year Member



Hi all.

I'm new to apache and mod_alias etc. My query is as follows - any help would be greatlt appreciated :

Users can access our site by specifying one of 2 URL's (slightly different due to language) - i.e they can access the same website using either of the example urls below -
[test.one.co.uk...] or
[zztest.zzone.co.uk...]
So DNS resolves both domains to the same application via the same http server (IHS).

We want to allow them to access the website application using only the domain name (not suffixed by /MySite/folder etc.)

I have changed the httpd.conf file as follows as an initial test :

RedirectMatch permanent ^/$ [test.one.co.uk...]

So now either url of
[test.one.co.uk...] or
[zztest.one.co.uk...]
will resolve to
[test.one.co.uk...]

What I need to do now, is maintain the orginal domain prefix in the redirected URL depending on which one they've used. Is there anyway we can code so that the destination redirect is prefixed with the domain that was first used, so that the browser url is always based on what they first used
I.e. -
[test.one.co.uk...] to
[test.one.co.uk...]
[zztest.one.co.uk...] to [zztest.zzone.co.uk...]

This is purely a superficial requirement as the way the redirect currently works the user is still accessing the application correctly, only the URL is displayed incorrectly if they first access via the different domain name.

Thanks!

jdMorgan

3:58 pm on Mar 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use mod_rewrite, rather that mod_alias. This will allow you to back-reference the %{HTTP_HOST} variable and put its value into the substitution URL. In httpd.conf, use something like:

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^((test¦zztest)\.example\.co\.uk)
RewriteRule ^/$ http://%1/MySite/folder/home.jsp [R=301,L]

If there are no other hostnames resolving to this particular ServerName container, the RewriteCond will not be needed, and the last two lines above can be replaced with this one:

RewriteRule ^/$ http://%{HTTP_HOST}/MySite/folder/home.jsp [R=301,L]

Note however, that this 'exposes' the home.jsp filepath in the URL, and you might be happier doing an internal rewrite, rather than an external redirect. In that case, the code would look like this:

Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^(test¦zztest)\.example\.co\.uk
RewriteRule ^/$ /MySite/folder/home.jsp [L]

This leaves the URL in the browser's address bar as-is, and simply serves the content for the requested URL-path matched on the left from the filepath shown on the right.

And again, the RewriteCond is only needed if more than just the two hostnames shown resolve to the current httpd.conf ServerName container.

Replace the broken pipe "¦" characters above with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim