Forum Moderators: phranque

Message Too Old, No Replies

Redirect Select URLs from Subdomain to WWW

         

clickfire

4:55 am on Apr 24, 2012 (gmt 0)

10+ Year Member



I have been trying to redirect individual URLs from the subdomain to varying desired locations on the root. I need to do this from within the root htaccess because the subdomain is going away. This is an example of what I've tried but nothing happened:

RedirectMatch permanent subdomain.domain.com/tag/blogging/$ http://www.domain.com/

g1smd

6:31 am on Apr 24, 2012 (gmt 0)

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



You have to do this in the subdomain htaccess.

The required pattern matching can see only the "path" part of the request, never the host.

lucy24

7:25 am on Apr 24, 2012 (gmt 0)

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



Redirects can't see the domain name. You have to use mod_rewrite [httpd.apache.org] and add a condition looking at the %{HTTP_HOST}. And the htaccess has to be in a place where requests for the subdomain can "see" it. Is the subdomain in a directory within the root directory, or are they side by side? I mean their real location, not the URL people use.

:: pause to laugh hysterically after clicking "Mapping URLs to the filesystem" link in See Also section of mod_alias page ::

Oh, and then: If you have any existing Redirects that use mod_alias, convert them to mod_rewrite because it is perilous to have both in an htaccess.

clickfire

5:06 pm on Apr 28, 2012 (gmt 0)

10+ Year Member



Okay, this is what I am trying in the root htaccess for one individual URL based on your remarks above. And, yes, the subdomain is in a directory within the root. Nothing happens with the below. How far off am I?

# blog.clickfire.com individual URLs to new www locations
RewriteCond %{HTTP_HOST} ^blog.clickfire.com/tag/blogging/$
RewriteRule ^http://www.clickfire.com/viewpoints/blogging/$1 [R=302,L]

lucy24

7:43 pm on Apr 28, 2012 (gmt 0)

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



How far off am I?

Mmmm ... 'bout a mile and a quarter. The {HTTP_HOST} is only the domain name, like www.example.com. Everything else is part of the {REQUEST_URI} or, by preference, included in the Rule itself.

Does your quoted rule get a 501? If not, you may color me surprised, since it appears to include both the pattern (the ^ anchor) and the target (the protocol-and-domain http://www.example.com) in a single word.

clickfire

8:13 pm on Apr 28, 2012 (gmt 0)

10+ Year Member



I tested it again and I didn't find any trace of a 501.

What should the %{HTTP_HOST} condition look like?

g1smd

9:25 pm on Apr 28, 2012 (gmt 0)

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



It should contain only the hostname you want to match, not any other part of the URL. Literal periods in that pattern should be escaped.

The rule should have a pattern and a target. The pattern part of this appears to be missing.

clickfire

5:39 am on May 6, 2012 (gmt 0)

10+ Year Member



Ok, is this getting closer? I tried it in the root with no results or errors.

# blog.clickfire.com individual URLs to new www locations
RewriteCond %{HTTP_HOST} ^blog.clickfire.com$
RewriteRule ^(.*)/tag/blogging/$ http://www.clickfire.com/viewpoints/blogging/$1 [R=302,L]

g1smd

6:28 am on May 6, 2012 (gmt 0)

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



If that code is placed in the htaccess file located at
blog.clickfire.com/.htaccess
then it should work for all URLs that end "...blogging/".

Escape the literal periods in the RegEx pattern.

Why do you want a 302 redirect and not a 301 here?

clickfire

6:52 am on May 6, 2012 (gmt 0)

10+ Year Member



The subdomain is going away so my hope was to get it in the root htaccess. I'm starting to wonder if it is even possible.

I always use 302 in the testing stages and will switch that to 301 if I can just get it to work. Thanks.

lucy24

7:44 am on May 6, 2012 (gmt 0)

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



Back up. What do you mean by "going away"?

#1 You're discontinuing it
#2 Your host has stopped supporting subdomains
#3 The directory containing the subdomain files is moving to another physical location
#4 et cetera

If you simply mean that you are discontinuing the subdomain, but its physical directory is still where it always was, then there should be no problem putting the rule in the root. Anything going to the subdomain has to pass the root directory along the way.

Someone else is going to explain how subdomains work at the DNS level. I'm not sure whether you have to keep a physical directory so there will be something for the requests to point to-- even if you plan on intercepting those requests before they ever get that far.

clickfire

5:01 pm on May 6, 2012 (gmt 0)

10+ Year Member



It's #1 and I don't want the baggage. If I have to keep the physical directory lying around forever just to do the redirects correctly, I will. I hadn't intended to keep it.

Also, I assumed that not having subdomain dns wouldn't matter because the redirect code should be returned regardless. Someone feel free to correct me on that point.

lucy24

9:11 pm on May 6, 2012 (gmt 0)

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



You may have to keep the directory, but you don't have to keep its content unless it's easier just to leave it there and forget about it. Doesn't matter one way or the other.

When I said "someone else" I wasn't kidding: I thought subdomains were done server-side, but if I try asking for a nonexistent subdomain I get the browser's DNS error message ("can't find server"), not the server's 404 ("no such place").

If the directory is physically located inside the directory that holds your primary domain, then all requests for the subdomain will hit your primary htaccess first. That is, they don't teleport directly to the subdomain's directory. Same principle as your overall domain: even though the DNS points to your own directory, requests still have to pass through the server's config file.

Oops. Er. Now, what was the question again?

clickfire

4:01 am on May 16, 2012 (gmt 0)

10+ Year Member



Okay, I ended up keeping the subdomain and using a regular redirect. Thanks for helping me work through this.

RedirectMatch permanent ^/tag/blogging/$ http://www.clickfire.com/$1

g1smd

11:09 pm on May 16, 2012 (gmt 0)

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



A RewriteRule with correct R=301 and L flags should and would have done the same job.