Forum Moderators: phranque

Message Too Old, No Replies

RedirectMatch

works with a trailing slash?

         

dhiggerdhigger

11:49 am on May 3, 2006 (gmt 0)

10+ Year Member



I'm using this rule in my .htaccess on Apache 2.0:

RedirectMatch ^/~me/mydirectory/(.*) http://www.example.com/$1 [L,R=301]

to redirect from
www.domain.com/~me/mydirectory/

to
www.example.com/
which is a "virtual host" or something. Anyway - same IP, same physical files, but different URL is what I mean.

The redirect is fine, except for when browsing to
www.domain.com/~me/mydirectory
(note the absence of a trailing slash).

In this case, the redirect sends my browser to
http://www.example.com//my/physical/server/path/me/WWW/mydirectory
with a 404 http header.

Can the rule be changed to work without the slash as well?

dhiggerdhigger

1:24 pm on May 3, 2006 (gmt 0)

10+ Year Member



I've discovered something more about this trailing slash issue.

I have this also in my .htaccess, to force a canonical hostname [httpd.apache.org] (force "www."):

RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]

These lines have the desired effect, and example.com is rewritten to www.example.com.

And then the RedirectMatch:

RedirectMatch ^/~me/mydirectory/(.*) http://www.example.com/$1 [L,R=301]

If I remove the lines which force the canonical hostname, the RedirectMatch command works when I browse to
www.domain.com/~me/mydirectory
(note absence of trailing slash)
The behaviour descibed above does not occur - I get redirected properly. I'd like to keep the "force canonical hostname" lines though!

jdMorgan

2:23 pm on May 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest you re-code the RedriectMatch to use mod_rewrite, so that there is no question of execution order. As it is now, the two modules -- mod_alias and mod_rewrite, will execute in whatever order the host has configured (you *cannot* control the order of your .htaccess directive execution, except on a per-module basis).

If you replace the RedirectMatch with a RewriteRule, then you can enforce an execution order between your two redirects.

Jim

dhiggerdhigger

2:51 pm on May 3, 2006 (gmt 0)

10+ Year Member



Hi Jim,

Thanks for your help. I actually had started writing the RedirectMatch in a RewriteRule, but I couldn't get it to work.

But by playing around I did get this to work:

RedirectMatch ^/~me/mydirectory/(.*) http://www.example.com/$1 [L,R=301]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]

The difference seems to be a test for just "example.com" rather than the absence of www.example.com.

I don't know what the line
RewriteCond %{HTTP_HOST}!^$
does though. What does it mean?

Dave