Forum Moderators: phranque
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?
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!
If you replace the RedirectMatch with a RewriteRule, then you can enforce an execution order between your two redirects.
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