Forum Moderators: phranque

Message Too Old, No Replies

replacing RedirectMatch with a conditional RewriteRule

a syntax clarification request

         

dhiggerdhigger

9:32 am on Mar 3, 2004 (gmt 0)

10+ Year Member



Hi folks, I realise there's masses on using RewriteRule in this and other forums, but after reading all day I still can't seem to get my syntax right.

My website has recently been changed to be virtually hosted (with a domain name all of it's own) but on the same server as before. So all the old URLs still automatically serve the correct pages, because they point to the "real" resource locations.
I.e. [medphysics.leeds.ac.uk...]
becomes [revisemri.com...]

I am simply trying to redirect all old-URL requests to the new URL form. I have accomplished this with RedirectMatch:
RedirectMatch ^/~dmh/mri/(.*) [revisemri.com...] [R=301,L]

This would be fine except some one of my cgi-bin programs doesn't work. I would therefore like to exclude the cgi-bin directory from the URL redirect instruction. Could anyone tell me why the following doesn't accomplish this? (I get an 500 internal server error on all pages for either domain)

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)/~dmh/mri/cgi\-bin/(.*) [S=1]
RewriteRule ^(.*)/~dmh/mri/(.*)$ [revisemri.com...] [R=301,L]

The server is Apache/1.3.22 (Unix).
Thanks alot
Dave Higgins

*******************************ADDED:
Got it!

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI}!^(.*)/~dmh/mri/cgi\-bin/(.*)
RewriteRule ^(.*)/~dmh/mri(.*)$ [revisemri.com$2...] [R=301,L]

Note the exclamation mark, essentially meaning NOT.

HOWEVER, even though the redirect rule is specified in the text R=301, the address
is not rewritten in the client browser, which kind of defeats the point, really. Or is it actually not working?

Any ideas?
Thanks
Dave

closed

3:15 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



Welcome to Webmaster World, dhiggerdhigger!

Well, since the address isn't changing, I don't think it's working.

Shouldn't it be cgi-bin instead of cgi\-bin?

jdMorgan

4:49 pm on Mar 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In an .htaccess context, there is a subtle difference between a URI tested by REQUEST_URI in a RewriteCond and the same URI tested in a RewriteRule -- The leading slash is tripped in RewriteRule. Also, I'd suggest you don't create back-references (using parentheses) unless you intend to use them:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{REQUEST_URI} !^/~dmh/mri/cgi-bin/
RewriteRule ^~dmh/mri(.*)$ http://www.example.com$1 [R=301,L]

Jim