Forum Moderators: phranque

Message Too Old, No Replies

Rewriting dummy subdomains - multiple rules don't work

Desperately needing help with subdomain redirects

         

sothis

6:28 pm on Jun 30, 2007 (gmt 0)

10+ Year Member



Jim, I've read all of your helpful advice on a ton of threads in here, and am hoping you know the easy solution for my problem.

(note: I've read toooons of these threads, both here and elsewhere, and spent hours trying to get this to work, sigh!)

There are a few things I need to accomplish:

1. http://forum.example.com or http://forums.example.com should change to http://www.example.com/forum/

2. http://subdomain.example.com should change to http://www.example.com/users/subdomain.html, but user should continue to see http://subdomain.example.com in their URL.

2. http://example.com should change to http://www.example.com

I can get 1 and 3 to work by using the following:

#forum redirect (I do this one first so it's taken care of, and then anything else is caught by the next rule)

RewriteCond %{HTTP_HOST} ^forum\.example\.com [OR]
RewriteCond %{HTTP_HOST} ^forums\.example\.com
RewriteRule ^(.*)$ http://www.example.com/forum/$1 [R,L]

#www versus nothing redirect
RewriteCond %{HTTP_HOST}!^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R,L]

However, I can't get the other rule to work basically ever. I've tried a TON of things, but I'll post the most recent:

RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
RewriteRule ^http://www.example.com/users/$1.html [L]

This one doesn't work... it redirects simply to www.example.com (even without the above "www versus nothing redirect" rule).

I've done other versions where i get a forbidden error for users/.html (it isn't inserting the subdomain part), all sorts of things.

Any ideas? I would REALLY appreciate it so I don't throw the laptop out the window. :)

thanks!

jdMorgan

7:22 pm on Jun 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# Redirect non-canonical domain to canonical "www" domain
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Redirect non-canonical "forums" subdomain to canonical "forum" subdomain
RewriteCond %{HTTP_HOST} ^forums\.example\.com [NC]
RewriteRule (.*) http://forum.example.com/$1 [R=301,L]
#
# Internally rewrite "forum" subdomain to /forum subdirectory
RewriteCond $1 !^forum/
RewriteCond %{HTTP_HOST} ^forum\.example\.com
RewriteRule (.*) /forum/$1 [L]
#
# Internally rewrite username subdomains to /users/<username>.html
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^a-z0-9\-]+)\.example\.com
RewriteRule !^users/ /users/%1.html [L]

If you're having trouble figuring this stuff out, take the code above apart character-by-character (no skipping ahead allowed) and look everything up in the documents cited in our forum charter [webmasterworld.com], Also, see the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

The new redirect rule is to force canonicalization of the "forum" subdomain. Just like "www" and "non-www", misspelled subdomains that resolve to the same content as a correctly-spelled subdomain may result in duplicate-content-related problems, so this rule is needed as well.

The extra RewriteCond on the internal rewrite is needed to prevent an "infinite" rewrite loop. The rules will rewrite a subdomain to a subdirectory, but only if we haven't already rewritten to that subdirectory within the current HTTP request. (To save you some time, "!" means "NOT".)

You may have trouble if you use page-relative links to include images, media, external JavaScripts, or stylesheets in your /forum or /users pages. Use absolute URLs or server-relative URLs to avoid this problem.
For example, use <img src="http://www.example.com/images/logo.gif"> or <img src="/images/logo.gif"> on your pages instead of <img src="images/logo.gif">

Be aware also that each and every subdomain will need it's own robots.txt, favicon.ico, content rating, privacy policy, and sitemap files -- These subdomains will be treated as separate Web sites, so if you use any of these files, they'll need to exist in all subdomains. In many cases, you can "share" one physical file by modifying the rewrites above so that the file's URL is excluded from being rewritten on a per-subdomain basis; Then the file would appear to be "shared" by all subdomains.

Jim

sothis

4:48 am on Jul 2, 2007 (gmt 0)

10+ Year Member



Hi Jim,

Thanks so much for the response. Everything you have written makes sense to me (I have a programming background and have read up extensively on mod_rewrite, but troubleshooting with this stuff is tricky :)).

However, the subdomain is still not redirecting properly. I tried removing everything except the last item, but regardless of what I type in (http://test.example.com), every page is redirected to the home page of my site. I've also tried changing the item to link to somewhere simple and obvious (ex: /forum/), and tinkered with other things, and it doesn't work.

I've read tons of tutorials, but at this point I'm wondering if something is just not set up on the server correctly. Writing redirects for things like example.com/forum might work, but perhaps something special has to be set to do domains?

I don't own the server my site is hosted on, and thus am not terribly knowledgable about what needs to be set. I do know it's a *nix box, that I'm on my own IP (at first I wasn't, and nothing was working so it was changed). If you have any ideas on a direction I might take, I'd greatly appreciate it.

jdMorgan

6:12 pm on Jul 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, back up and start with something simple. Does this work?

RewriteRule ^foo\.html$ http://www.webmasterworld.com/ [R=301,L]

if so, then when you request the page "foo.html" from your server, you should land back here at WebmasterWorld.

If not, then there is something very basic that is missing or incorrect, and it would be a waste of time to do detail-level code troubleshooting on a server that can't do a simple redirect.

Jim

sothis

6:30 pm on Jul 2, 2007 (gmt 0)

10+ Year Member



Indeed it does. Any of the forum redirects work, as well as other old redirects I had set up for additional pages (these are very specific, and also deal with child-level folders such as example.com/somethingelse/).

The reason why I wonder if it's a server issue is because literally the only redirects that never seem to work are ones which involve a subdomain (***.example.com). Admittedly though, a redirect rule for http://example.com -> http://www.example.com does work, so I'm not sure why anythingelse.example.com would not also work.

jdMorgan

7:58 pm on Jul 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are the subdomains defined in DNS, either individually, or as a wild-card?

Have you used a "control panel" to "create" these subdomains? If so, each will be mapped to it's own filespace, and no "centralized" code can be used to rewrite/redirect them. Basically, the control-panel code assumes you want separate, static subdomains, each given its own subdirectory to put its files into. And in some cases, hosts who make you use their control panel make no provisions for wild-card subdomains, making the creation and use of dynamic subdomains impossible on their servers.

Any more info about this? -- It's a bit strange...

Jim

sothis

8:07 pm on Jul 2, 2007 (gmt 0)

10+ Year Member



Nothing has been set up for the DNS (this server runs CPanel)... is that necessary? I was under the impression that the only reason you'd want to do that is to be able to give users their own physical space. I simply want a way to give people "fake" subdomains, so that they have an easier homepage to remember. Then again, forum/forums aren't set up in the DNS either, but those redirects work. Strange indeed :/

jdMorgan

8:19 pm on Jul 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you either have to define each subdomain in DNS, or simply specify that *all* subdomains should resolve to your server's IP address. Then the server --if name-based-- must be told what hostnames it should accept and what to do with them. In both cases, you have a choice as to what to declare, but you must declare something.

If you request several random subdomain and get 'domain not found' or similar connection-level errors, then that would indicate that DNS is not set up for wild-card subdomains. Otherwise, it's safe to assume that it is already set up. But I have to ask... :)

Have a look at the cPanel and see if perhaps you haven't pointed the new subdomains away from the main domain's filespace. I regret to say that cPanel-based hosts aren't very good for what you're trying to do... If there is no "define wild-card subdomain" function in it, then this may not work at all.

Basically, cPanel generates canned code snippets and inserts them into httpd.conf and .htaccess. This allows non-technical Webmasters to 'safely' make certain configuration changes, but it also severely limits what *can* be done with the server. Personally, I look for hosting that *does not* include or require the use of a control panel when I want to do anything but host a plain-jane single-domain site...

In this case it may be that by default, the server config handles www- and non-www domains, and that the forum installation script contains code to define its own forum subdomain, but that all other subdomains will have to be defined through the control panel -- and that's not what you'd want for a project like this.

Jim

sothis

8:31 pm on Jul 2, 2007 (gmt 0)

10+ Year Member



Ohhhhh!

Ok, my apologies... I do think my server guy set up the wildcards (heearing about it just rang a bell). I remember this because for awhile, the redirects wouldn't hit the server at all... and once it was set up, they worked to some extent.

So then, it sounds like what you are saying is that because wildcards are set up, that there is no way to redirect? (Unless I physically create thousands of specific rules and directories?

thanks again for all your help!

jdMorgan

9:00 pm on Jul 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, that's an over-simplification of what I said -- and what I didn't say, because it would take a book to cover all of this...

Since you have a "server guy", I think your best bet is to get with him, tell him what you're trying to do, and get the server configuration fixed-up so that you *can* do it -- Basically, you want any and all subdomains to resolve to your main DocumentRoot directory, and then you can handle sorting them into folders using the code above in an .htaccess file located in that directory.

Your server guy has the 'power' to make these changes, and presumably, also has some familiarity with your site and with your goals.

Jim

sothis

9:27 pm on Jul 2, 2007 (gmt 0)

10+ Year Member



Alas, I wish it were that simple :)

My "server guy" is a friend who owns a server, and gives me a good discount to host with him. However, he doesn't know much about mod_rewrite and often if I want him to make any server changes, he has me get him the exact query or thing to do.

In this case, he's done all that he knows how, but I think I'll need to find the solution myself in order for him to implement it.

I did get this information from him, that he did set wildcards:

*.example.com. 14400 IN A (my site's static IP)

It does seem very strange that forums/forum/(nothing or) WWW would work, yet not any other string. The only other thing I can think of is that the rule isn't being caught (even if it's the only thing in the file, which makes no sense), and thus is defaulting to the wildcard setting and pushing the browser to the root directory.

That, or there's some other server setting I'm unaware of which lets you redirect "fake" subdomains to a real address.

jdMorgan

1:45 am on Jul 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's absolutely nothing fake about these subdomains...

You've established that wildcard DNS is set up. Now ask your server guy if there is a wildcard SeverAlias in your httpd.conf file to tell the server what to do with these other subdomains -- They should all point to wherever your subdomain-rewriting .htaccess file resides. I suspect you may find that only your domain, www.yourdomain, and forum.yourdomain are defined.

Jim

sothis

7:40 pm on Jul 5, 2007 (gmt 0)

10+ Year Member



Success! That was the problem.. he had set up the DNS wildcard, but NOT ServerAlias. Once he added that wildcard, the subdomain redirects now work! THANK YOU!

I do have one follow up question, if it isn't too much trouble.

I ended up using the following code to redirect the subdomains (the code you had mentioned didn't work quite as expected, but this code does work):

RewriteCond %{HTTP_HOST} !^www\.example\.com?$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com?$
RewriteRule ^$ /users/entry.php?id=%1 [L]

(^^I changed the redirect to the simple .php? one, because I realized something.html interfered with another rewrite rule I've had in place for awhile now)

The problem is, I need the URL to appear to be username.example.com ONLY UPON the first redirect. If they click anywhere, the URL should return to www. This isn't as simple as looking for any click inside the /users/ directory and keeping that with the subdomain URL, because I literally only want the subdomain to show up on the one page (entry.php... which is also rewritten at times to be /users/###.html (where # is a number).

I worked for about 3 hours with a friend of mine who is very skilled with mod_rewrite, and both of us are completely stumped. We've tried tons of things, but here's just one example that didn't work:

RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^\/(.+) http://www.example.com/$1 [L,R]

^^this is listed above the other rule for redirecting the subdomain.

If you have a minute, do you know of how to accomplish this task?

Thank you *so much*, again.

[edited by: jdMorgan at 8:32 pm (utc) on July 5, 2007]
[edit reason] example.com [/edit]

jdMorgan

8:48 pm on Jul 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I strongly suggest you abandon this idea, because if you expose the user's subdirectories in the URL by using an external redirect, then you invite them to 'peek' into each other's space through the back door you've opened...

Otherwise, you can make use of a RewriteCond examining %{REQUEST_URI} and looking for NOT ^/$
So if the request is for a user subdomain and is NOT for the "/" page, then redirect to the www domain, prepending the user subdirectory path to the requested page URL-path.

Jim