Forum Moderators: phranque

Message Too Old, No Replies

Need help with a simple mod_rewrite script...

I don't really know what I'm doing.

         

jrsmith

7:40 pm on Jun 3, 2005 (gmt 0)

10+ Year Member



I've been wrestling with this on and off for a couple days.

I need to redirect everything for [olddomain.com...] to [newdomain.com...] EXCEPT for a certain subfolder. Now, the tricky part is that while www.olddomain.com points to the ISP that hosts olddomain's site, olddomain.com (sans www) points to our company firewall, I presume for email purposes.

So, the below script has been hacked together from various examples and tidbits I've been able to find online. It works fine for the following example URLs:


http://www.olddomain.com/ #redirect to http://www.newdomain.com/
http://www.olddomain.com/some_file #redirect to http://www.newddomain.com/some_file
http://www.olddomain.com/subdir/ #ignores and brings up http://www.olddomain.com/subdit/

But if I try to go to [olddomain.com...] WITHOUT a trailing slash, it redirects me to [olddomain.com...] WITH the trailing slash but WITHOUT www. I don't understand why.

I humbling beg your guidance, gurus. I know that with a script this small, it has to be some stupid mistake on my part or some fundemental understanding that I lack. Any help is appreciated.. Here's the current script:


RewriteEngine on
RewriteCond %{REQUEST_URI}!^/subdir/?$ [NC]
RewriteRule (.*) http://www.newdomain.com/

PS - I read somewhere about testing for null HTTP_HOST values when using negative pattern matching.. Is that necessary here?

jdMorgan

8:39 pm on Jun 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jrsmith,

Welcome to WebmasterWorld!

> olddomain.com (sans www) points to our company firewall, I presume for email purposes.

Only the MX record should point to the e-mail server; You should have either a separate A record for "domain.com.", or use a wildcard A record "*.domain.com." to point to the Web server.

Your rules should fail on this test:
http://www.olddomain.com/some_file #redirect to http://www.newddomain.com/some_file
since there is no provision for back-referencing the requested page "some_file"

> But if I try to go to http://www.olddomain.com/subdir WITHOUT a trailing slash, it redirects me to http://olddomain.com/subdir/ WITH the trailing slash but WITHOUT www. I don't understand why.

This sounds like a configuration setting (UseCanonicalName [httpd.apache.org]) on olddomain, being invoked by mod_dir [httpd.apache.org]. You'll need to investigate httpd.conf on the old server.


RewriteEngine on
# if requested URI does not start with "/subdir"
RewriteCond %{REQUEST_URI} !^/subdir [NC]
# redirect all requests to www.newdomain with requested page intact.
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

For some basic references to mod_rewrite info, see our forum charter [webmasterworld.com].

Jim

jrsmith

9:08 pm on Jun 3, 2005 (gmt 0)

10+ Year Member



jdMorgan,

UseCanonicalName
appears to have been the culprit. Unfortunately, the old domain is on shared hosting, so changing that was not an option. I did, however, find an earlier post [webmasterworld.com] of yours that provided a solution. I know it's not ideal, but the use of this will be somewhat limited, so it's going to do the trick, I think.

I can't thank you enough. I'll try to talk the IT Director into donating to these forums.

JR