Forum Moderators: phranque

Message Too Old, No Replies

How to get redirects in .htaccess to *exclude* a subdomain?

Can't quite figure out to do this. Help...?

         

dduane

7:27 pm on Feb 23, 2007 (gmt 0)

10+ Year Member



Hi, all!

I've just installed a longish list of "redirect 301's" in the .htaccess file of one of my old domains, to send visitors to equivalent pages in the new one. Most of the redirects look like this:

Redirect 301 /index.html http://www.example.com/node
Redirect 301 /YWExcerptPage.html http://www.example.com/young-wizards-current-chapter-excerpt
Redirect 301 /YWMakeoverIntroToTheWorld.html http://www.example.com/About-The-Young-Wizards-Universe
Redirect 301 /YWWizardsHoliday.html http://www.example.com/Wizards-Holiday-Mass-Market-Edition
Redirect 301 /YWWizardsAtWar.html http://www.example.com/Wizards-At-War-Mass-Market-Edition
Redirect 301 /YWTheWizardsDilemma.html http://www.example.com/The-Wizards-Dilemma-Mass-Market-Edition
Redirect 301 /YWTheBooks.html http://www.example.com/the-books
...

Etc.

All of these redirects are from files in the "public_html" root. Now, everything's working fine, and all the pages are redirecting correctly, except for one detail...

I have a subdomain, wcast.YW.net, where podcasts are kept. Because of some bandwidth issues, I don't want to move or redirect this subdomain / subdirectory to the .com side. I had thought that simply avoiding specifying it in the list above would mean that it wouldn't redirect. Nonetheless, it's redirecting to the "index" page of the other domain -- this URL:
http://www.example.com/node

Is that initial redirect of the old site's index.html to the YWCom "index" somehow to blame for this? Or is there something else going on that I don't know about / understand (because I'm very new at this)? Or have I screwed something up in some interesting way? And can someone suggest syntax by which I can stop the "wcast" subdomain from redirecting, and tell .htaccess that I do *not* want this subdirectory redirected, but logins to that URL should proceed as they have until now? (The subdomain is mapped to /wizcast, under the root.)
All advice gladly accepted.

Best -- Diane

[edited by: jdMorgan at 8:46 pm (utc) on Feb. 23, 2007]
[edit reason] Removed specifics per Terms of Service [/edit]

jdMorgan

9:02 pm on Feb 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Other than that implicit in its directory location, the Redirect directive has no "knowledge" of what (sub)domain is being requested, so any requested URL that matches a Redirect prefix-URL will be redirected. There is no solution using mod_alias.

However, Apache mod_rewrite [httpd.apache.org] can be used to address this problem with a "long list" of redirects, using an "abort-on-match" code structure, or a "skip on match" structure, whichever you find easier to implement and maintain. The skip structure requires that you accurately count the rules to be skipped, while the abort structure and rules must be placed after any other rules which might apply to the current URL request -- Otherwise, they'll never be run for the subdomain.

Here are both methods (near the top) -- Use only one.


# Enable mod_rewrite
Options +FollowSymLinks
RewriteEngine on
#
# Stop mod_rewrite processing if "wcast" subdomain requested
RewriteCond %{HTTP_HOST} ^wcast\.example.net
RewriteRule .* - [L]
#
# Skip next seven rules if "wcast" subdomain requested
RewriteCond %{HTTP_HOST} ^wcast\.example.net
RewriteRule .* - [S=7]
#
RewriteRule ^index\.html$ http://www.example.com/node [R=301,L]
RewriteRule ^YWExcerptPage\.html$ http://www.example.com/young-wizards-current-chapter-excerpt [R=301,L]
RewriteRule ^YWMakeoverIntroToTheWorld\.html$ http://www.example.com/About-The-Young-Wizards-Universe [R=301,L]
RewriteRule ^YWWizardsHoliday\.html$ http://www.example.com/Wizards-Holiday-Mass-Market-Edition [R=301,L]
RewriteRule ^YWWizardsAtWar\.html$ http://www.example.com/Wizards-At-War-Mass-Market-Edition [R=301,L]
RewriteRule ^YWTheWizardsDilemma\.html$ http://www.example.com/The-Wizards-Dilemma-Mass-Market-Edition [R=301,L]
RewriteRule ^YWTheBooks\.html$ http://www.example.com/the-books

As written, this code is intended for use in .htaccess; Add a leading slash to each RewriteRule pattern for use in httpd.conf or conf.d

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

MichaelBluejay

9:02 pm on Feb 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Where is your .htaccess file located? On my server every domain and subdomain has its own folder, like this:

/home/user/domain.com
/home/user/sub.domain.com
/home/user/sub2.domain.com

The .htaccess file controls everything below it. So if you put it here:

/home/user/.htaccess

It's gonna affect all your subdomains.

But if you put it here:

/home/user/domain.com/.htaccess

Then it affects only domain.com.

Hope that helps.

dduane

10:44 pm on Feb 23, 2007 (gmt 0)

10+ Year Member



Jim -- Many thanks: I'll mull that over and see how it can best be made to work. I will say it's confusing-looking at first glance, but then I've spent the last couple of weeks of this odyssey into redirection being confused, and it hasn't killed me yet. :)

Michael -- I wish my directory structure was as straightforward as yours: otherwise I wouldn't be having these problems. ;) What I have is this:

/
¦__ public_html
¦__ mail
¦__ imap
¦__ domains

"public_html" contains my web root, and the .htaccess. The various subdirectories are "below" it:

¦__ wzcast
¦__ cgi
¦__ images
¦__ email-username

...etc. However, the "domains" directory structure goes like this:

yw.net
¦__ public_html
¦__ logs
¦__ stats
¦__ private_html
¦__ public_ftp
¦__ .htpasswd

-- and the second "public_html" directory is identical to the first, containing my .htaccess file and everything else. There are no other domain/subdomain directories under "domains" except for the yw.net one.

So you see why I'm confused. I'm wondering whether perhaps I should be putting the .htaccess file "higher up", say in the domains directory. Yet the directory I'm trying to keep from being redirected is below "domains" as well, and should still be affected.

(sigh) Any thoughts?

Best -- Diane

jdMorgan

10:50 pm on Feb 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In case it wasn't clear from the documentation, with RewriteOptions inherit specified, an .htaccess file affects the directory that it resides in, and all subdirectories below that directory.

Jim

dduane

9:27 am on Feb 24, 2007 (gmt 0)

10+ Year Member



I got that, Jim: thanks!

Best -- Diane

dduane

7:20 pm on Feb 24, 2007 (gmt 0)

10+ Year Member



Thanks to both of you -- the problem's solved. Jim, I wound up using the "abort-if" option, which when placed at the end of the redirect list, works perfectly.

Thanks again for your help!

Best -- Diane

MichaelBluejay

12:51 am on Feb 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Glad you got it working. Your directory structure is certainly confusing. Having a clean directory structure is one of the main reasons I've stuck with my current host rather than going to one with better support. I've got lots of users, domains, and subdomains, and it's important to me that it be easy to use -- so much so that I won't even use a host that doesn't give me a clean directory structure.